【问题标题】:Strong parameters - Devise 3.0.0 and Rails 4. "Unpermitted parameters: name"强参数 - 设计 3.0.0 和 Rails 4。“不允许的参数:名称”
【发布时间】:2013-11-12 07:01:08
【问题描述】:

我目前正在使用 Rails 4 和 Devise 3.0.0。我试图在注册表单和编辑注册表单中添加一个“姓名”的自定义字段。每当我提交表单时,都会出现以下错误:

Unpermitted parameters: name

WARNING: Can't mass-assign protected attributes for User: email, password, password_confirmation.

我知道这与 Rails 4 处理参数的方式有关,但我不明白我现在应该怎么做。我四处搜索,发现我应该在涉及“参数”的用户模型中添加一些行。

我的用户模型目前如下所示:

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable, #:recoverable,
          :rememberable, :trackable, :validatable

  attr_accessible :name, :password, :password_confirmation, :remember_me, :email
end

根据How is attr_accessible used in Rails 4?,我应该将以下代码添加到“控制器”中。

class PeopleController < ApplicationController
  def create
    Person.create(person_params)
  end

  private

  def person_params
    params.require(:person).permit(:name, :age)
  end
end

什么控制器?这是文字代码吗?由于我正在与用户打交道,我必须使用 User.create(user_params) 吗?而不是 Person.create(person_params)?

【问题讨论】:

    标签: ruby-on-rails ruby devise


    【解决方案1】:

    Rails 4 已将参数清理从模型移至控制器。 Devise 处理 3 个操作,sign_in、sign_up 和 account_update。对于sign_up,允许的参数是authentication key(默认为:email)、passwordpassword_confirmation

    如果您想将:name 添加到User 模型并将其用于注册,请在/config/initializers/devise.rb 中将config.authentication_keys = [ :email ] 更改为config.authentication_keys = [ :name ],或者如果您想同时使用:email 和@987654332 @,将此添加到ApplicationController

    class ApplicationController < ActionController::Base
      before_action :configure_permitted_parameters, if: :devise_controller?
    
      protected
    
      def configure_permitted_parameters
        devise_parameter_sanitizer.for(:sign_up) << :username
      end    
    end
    

    还检查- https://github.com/plataformatec/devise#strong-parameters

    【讨论】:

      【解决方案2】:

      您必须在您编写 User.create(user_params) 的控制器中添加它。我假设是 UsersController。

      class UsersController < ApplicationController
        def create
          User.create(user_params)
        end
      
        private
      
        def user_params
      #assumption: user params are coming in params[:user]
          params.require(:user).permit(:name, :age, :and_other_params_you_want_to_allow)
        end
      end
      

      【讨论】:

      • 由于某种原因,Rails 未能创建用户控制器。让我创建一个然后回复你。
      • 您能告诉我您的用户表单发布到的 URL 吗?
      • 不确定“用户表单发布到的 URL”是什么意思。但是,我创建了一个 users_controller.rb 并写在你提到的代码中,这就是我得到的:Unpermitted parameters: name WARNING: Can't mass-assign protected attributes for User: email @techvineet
      【解决方案3】:

      是的,您应该添加如下一行:-

      attr_accessible :name
      

      在您的模型中允许分配名称,如果它不起作用,请尝试此How is attr_accessible used in Rails 4?

      【讨论】:

      • 我刚刚将我的用户模型添加到问题中。我实际上确实有那条线。我现在会阅读您的链接。
      • 我已使用您提供的链接中的更多信息更新了问题。我不确定如何实现它所说的。
      【解决方案4】:

      我也有类似的问题。所以,为了解决这个问题,我创建了自定义注册控制器,继承了 DeviseRegistration 控制器。检查设计文档并像这样定义控制器。

      class RegistrationsController < Devise::RegistrationsController
        before_filter :update_sanitized_params, if: :devise_controller?
      
      
        def update_sanitized_params
         devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:name, :email, :)}
       end
      end
      

      确保您已在 config/routes.rb 中为此控制器定义此路由

        devise_for :users, :controllers => {:registrations => "registrations" } , :path => '', :path_names => {
          :sign_in => 'login', 
          :sign_out => 'logout'
        }
      

      Check this documentation of devise for strong parameter.

      【讨论】:

        【解决方案5】:

        我有类似的问题,这是我的解决方法:

        class ApplicationController < ActionController::Base
          before_filter :configure_permitted_parameters, if: :devise_controller?
        
          protected
        
          def configure_permitted_parameters
            devise_parameter_sanitizer.for(:account_update) { |u| u.permit!}
          end
        end
        

        【讨论】:

          猜你喜欢
          • 2021-12-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-11
          • 1970-01-01
          • 2013-08-01
          • 2023-03-23
          • 2013-07-25
          相关资源
          最近更新 更多