I am working on an app that uses Devise 3.2. I am trying to get some extra parameters through when saving a member
. According to the documentation I should be able to do this.
In my application_controller.rb I have this (Rails 3.2.22.5)
before_filter :configure_permitted_parameters, if: :devise_controller?
and then I am trying the two versions the documentation says should work.
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << [:name, :site_id]
devise_parameter_sanitizer.for(:accept_invitation) << [:name, :site_id]
end
But that does not work saying devise base sanitizer expects a Block
. Which, according to the documentation I should be able to pass, so I try this...
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :site_id, :email, :password, :password_confirmation) }
devise_parameter_sanitizer.for(:accept_invitation) { |u| u.permit(:name, :site_id, :invitation_token, :password, :password_confirmation) }
end
...and it gives me the error
undefined method permit for # <ActiveSupport::HashWithIndifferentAccess:0x007fbf17e00b40>
I think this is because everything is inside the :member => {...} param, but I am not sure of that and I cannot figure out the syntax if it is. I just need to be able to save a member with a name
and site_id
attribute.
The params look like this
{"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"d9pudkerHaJoP7H7N1vYzWs/SSX1WtRO+8lRD313xP0=", "member"=>{"invitation_token"=>"WLh98kdrFAnyVb879EDS", "name"=>"NAMEHERE", "password"=>"XXXXXXX", "password_confirmation"=>"XXXXXXX", "site_id"=>"XXXXXXXXXXXXX"}, "commit"=>"Set my password", "action"=>"update", "controller"=>"devise/invitations"}
Can anyone help me get this working? Thanks!
The permit
method is clearly available in devise as described in their documentation, however something is clearly wrong with it https://github.com/plataformatec/devise/blob/v3.5.4/lib/devise/parameter_sanitizer.rb#L65 since your code definitely doesn't work, as did mine.
You can also see why the non-block is raising that error here: https://github.com/plataformatec/devise/blob/v3.5.4/lib/devise/parameter_sanitizer.rb#L12-L18 which calls this https://github.com/plataformatec/devise/blob/v3.5.4/lib/devise/parameter_sanitizer.rb#L30-L32
The best way to solve this is to just use the gem strong_parameters
which is a subclass of ActiveSupport::HashWithIndifferentAccess` see: https://github.com/rails/strong_parameters#migration-path-to-rails-4
So simply add it to your gemfile and bundle install:
gem 'strong_parameters'
bundle install