문제

I'm using strong parameters gem on small project with devise

I have added User.rb a locale column. This is my controllers/users/registrations_controller.rb file:

class Users::RegistrationsController < Devise::RegistrationsController
  def resource_params
    params.require(:user).permit(:username, :email, :password, :password_confirmation, :locale)
  end
  private :resource_params
  def create
   #add params[:locale] to resource.locale here
   super
  end
end

These are the parameters received from form:

Parameters: {"authenticity_token"=>"ZyrtToHcwsX3zl2ive93cpYaom6HNGA/jnYcSg7pQUQ=", "user"=>{"username"=>"hyperrjas@hyperrjas.com", "email"=>"email@email.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create account", "locale"=>"es"}

I would like add to user :locale column the parameter params[:locale]

How can I do it?

Thanks!

도움이 되었습니까?

해결책

I have fixed the problem overriding the create method from this doc:

https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb.

If you want add specify column or field locale on your model user with strong parameters gem, you can add to controllers/users/registrations_controller.rb the next:

class Users::RegistrationsController < Devise::RegistrationsController

  def create
    build_resource
    resource.locale = set_locale #set_locale or where you have stored your own locale
    if resource.save
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_up(resource_name, resource)
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      respond_with resource
    end
  end

  def resource_params
    params.require(:user).permit(:username, :email, :password, :password_confirmation, :locale)
  end

  private :resource_params
end

Regards!

다른 팁

Option 1

Handle it in the view.

Your view probably has something like:

<%= form_for :user do |f| %>
  <%= f.username %>
  <%= f.password %>
  <%= # Etc... %>
<% end >
<%= select_tag :locale, options_from_collection_for_select(I18n.config.available_locales, :to_s, :to_s), :selected => user.locale %>

So let's change this to:

<%= form_for :user do |f| %>
  <%= f.collection_select :locale, I18n.config.available_locales, :to_s, :to_s, :selected => user.locale %>
  <%= f.username %>
  <%= f.password %>
  <%= # Etc... %>
<% end >

That way, the params object will already nest locale in user.

Option 2

Option 1's disadvantage is that your locale select might be in a navbar and it wouldn't make sense to move that into the form. However, you can maintain 2 fields--the existing navbar one, and a hidden on in the form.

<%= form_for :user do |f| %>
  <%= f.hidden_field :locale, value => user.locale %>
  <%= f.username %>
  <%= f.password %>
  <%= # Etc... %>
<% end >

And use Javascript to copy the value from the navbar field to the hidden field before submitting:

$('form').on('submit', function(){
  $("#user_locale").val($("#locale").val());
});

Option 3

Copy it over in the controller before setting your strong parameters:

class Users::RegistrationsController < Devise::RegistrationsController
  private
    def resource_params  
      params[:user][:locale] = params[:locale]
      params.require(:user).permit(:username, :email, :password, :password_confirmation, :locale)
    end
end

All of these are much better alternatives to the accepted answer because they avoid code repetition. You won't have to copy-and-paste again every time you want to upgrade devise.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top