问题
I have just added devise to my shiny new Rails 5 app. All's good and dandy until I try to add a username to the Devise user model. Everything worked until I ran it and went to localhost:3000/users/sign_up, where I was greeted by this error:
undefined method `for' for #<Devise::ParameterSanitizer:0x007fa0dc31c1b0> Did you mean? fork
I have searched the wonderful place of Google for any results, only being given outdated, Rails 4 errors and solutions, the same with searching Stack Overflow itself. I cannot get my mind to find a working solution. I would appreciate help very much. Here is how I prepared for this:
- Create the migration:
rails generate migration add_username_to_users username:string:uniq
- Migrate the database
rake db:migrate
- Add strong parameters to the application controller
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(..) }
- Add username parameters to the Devise views
- Restart the server
Is there anything I missed or did wrong?
回答1:
The .for method is deprecated, from devise 4.1+ .permit method is available
Try .permit.
It should work.
devise_parameter_sanitizer.permit(:my_action) { |u| u.permit(..) }
Hope this will help you :)
回答2:
The problem was well mentioned by @Shadow but this syntax worked for me:
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, :email])
devise_parameter_sanitizer.permit(:account_update, keys: [:first_name, :last_name, :phone, :email, bank_attributes:
[:bank_name, :bank_account]])
end
来源:https://stackoverflow.com/questions/38448598/devise-parameter-sanitizer-for-method-not-found-rails-5