2016-06-05 18 views
0

Ich Setup Devise in meiner Schienen-Anwendung. Ich habe auch ein Devise Parameter Desinfektionsmittel in meinem Application Controller eingerichtet. Aber wenn ich mich mit den benutzerdefinierten Parametern anmelde, erhalte ich einen "unerlaubten Parameterfehler" in meinem lokalen Server-Log. Das komische Problem ist, dass das auf einmal funktioniert hat ... und jetzt scheint es kaputt zu sein. Ich habe meine Devise-Konfiguration und die Dokumentation durchgesehen und den Sanitizer korrekt eingerichtet.Devise unzulässige Params Fehler

Application_Controller:

class ApplicationController < ActionController::Base 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    include Pundit 
    include Redcarpet 
    protect_from_forgery with: :exception 
    before_action :authenticate_user! 

    rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized 

    def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :firstname, :lastname, :username) } 
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation, :current_password, :firstname, :lastname, :username) } 
    end 
end 

Rails Server Log:

Started POST "/users" for ::1 at 2016-06-05 16:02:57 -0700 
Processing by Devise::RegistrationsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"PhMqJOEO7fLyUJiWjHcnu+wyB0EQwDeCV9m6XsT5kZ/IyVZ9ZUpwLc26sNbRZleh6xz7V90bvA+yqUBkDkhMmA==", "user"=>{"firstname"=>"Austin", "lastname"=>"Thesing", "username"=>"austinthesing", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"} 
Unpermitted parameters: firstname, lastname, username 
    (0.1ms) begin transaction 
    User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 
    SQL (0.4ms) INSERT INTO "users" ("email", "encrypted_password", "role", "created_at", "updated_at", "confirmation_token", "confirmation_sent_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["email", "[email protected]"], ["encrypted_password", "$2a$11$KQLR6rLh0qrMfzX90FToP.Yo.i0wmfPkvvw7JOKQIn4smtzk9HJkm"], ["role", 0], ["created_at", "2016-06-05 23:02:57.825074"], ["updated_at", "2016-06-05 23:02:57.825074"], ["confirmation_token", "HMkLUzBwoXB7NZxvYJCA"], ["confirmation_sent_at", "2016-06-05 23:02:57.825333"]] 
    (0.6ms) commit transaction 
    Rendered devise/mailer/confirmation_instructions.html.erb (4.9ms) 

Antwort

1

Sie benötigen einen before_action Rückruf angeben, die configure_permitted_parameters Methode in der Application Controller anzurufen, wenn der Controller ein devise ist Steuerung

class ApplicationController < ActionController::Base 
    before_action :configure_permitted_parameters, if: :devise_controller? 

    protected 

    def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :firstname, :lastname, :username) } 
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation, :current_password, :firstname, :lastname, :username) }  end 
end 

auch sehen, dass das Verfahren geschützt

Lesen Sie mehr über starke Parameter Devise here

+0

@elmagnifico Thank you !! Dieser before_action Callback sagt im Grunde: Wenn der devise_controller diese Methode zuerst ausführt .. Richtig? Danke noch einmal. – austinthesing

+1

Genau! Die Methode wird nur aufgerufen, wenn der Controller in Aktion ein Gerätecontroller ist. Bitte schön :) –

Verwandte Themen