0

我在设计的小项目上使用强参数gem

我添加User.rb了一个locale专栏。这是我的controllers/users/registrations_controller.rb文件:

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

这些是从表单接收的参数:

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

我想添加到user :locale column参数params[:locale]

我该怎么做?

谢谢!

4

2 回答 2

0

我已经解决了覆盖create此文档中的方法的问题:

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

如果你想column or field locale在你的模型用户上添加指定strong parameters gem,你可以添加到controllers/users/registrations_controller.rb下一个:

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

问候!

于 2013-02-24T10:35:49.370 回答
0

选项1

在视图中处理它。

您的视图可能类似于:

<%= 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 %>

因此,让我们将其更改为:

<%= 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 >

这样,params 对象就已经嵌套localeuser.

选项 2

选项 1 的缺点是您的语言环境选择可能位于导航栏中,将其移动到表单中是没有意义的。但是,您可以维护 2 个字段——现有的导航栏一个,以及一个隐藏在表单中的字段。

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

并在提交之前使用 Javascript 将值从导航栏字段复制到隐藏字段:

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

选项 3

在设置强参数之前将其复制到控制器中:

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

所有这些都是公认答案的更好选择,因为它们避免了代码重复。每次要升级设备时,您都不必再次复制和粘贴。

于 2014-07-10T14:48:39.130 回答