Devise Parameter Sanitizer “For” Method Not Found Rails 5

人盡茶涼 提交于 2019-12-23 17:33:41

问题


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:

  1. Create the migration: rails generate migration add_username_to_users username:string:uniq
  2. Migrate the database rake db:migrate
  3. Add strong parameters to the application controller devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(..) }
  4. Add username parameters to the Devise views
  5. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!