【问题标题】:allow set_locale with strong_parameters gem & devise允许 set_locale 与 strong_parameters gem & devise
【发布时间】:2013-02-23 12:47:47
【问题描述】:

我在带有设计的小项目上使用strong parameters 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]

我该怎么做?

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 strong-parameters


    【解决方案1】:

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

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

    如果你想用strong parameters gem在你的模型用户上添加指定column or field locale,你可以添加到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
    

    问候!

    【讨论】:

      【解决方案2】:

      选项 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 对象已经将locale 嵌套在user 中。

      选项 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
      

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

      【讨论】:

        猜你喜欢
        • 2023-03-12
        • 2013-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多