简体   繁体   English

设计3 Rails 4不允许的参数

[英]Devise 3 Rails 4 Unpermitted parameters

This is what I'm getting when signing up: Unpermitted parameters: password_confirmation WARNING: Can't mass-assign protected attributes for User: email, password I have installed everything and am trying devise out first time. 这是我注册时得到的: Unpermitted parameters: password_confirmation WARNING: Can't mass-assign protected attributes for User: email, password我已经安装了所有内容,并且正在尝试首次设计。 I have created a model user and everything should be smooth. 我已经创建了一个模型用户,一切应该顺利。 This is a fresh installation of devise first user sign up. 这是设计初次用户注册的全新安装。 No default code has been altered. 没有默认代码已被更改。 Please help! 请帮忙!

PROBLEM SOLVED: I added attr_accessible to my user.rb model 解决的问题:我向user.rb模型添加了attr_accessible

Check your User model and see if the parameters are accessible. 检查您的用户模型,并查看参数是否可访问。

Keep in mind you don't need to name them, IE my User model looks like this with devise 3 and rails 4: 请记住,您不需要命名它们,即IE我的用户模型如下所示,带有设计3和导轨4:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  validate :username, presence: true, uniqueness: true, format: { with: /[a-zA-Z0-9]{4,20}/ }
end

Solution Rahul is talking about doesn't make much sense as these parametters are already permitted on the requests, but in my case, I have an extra username attribute for this User model so I added to ApplicationController: 解决方案Rahul谈论的问题没有多大意义,因为请求中已经允许使用这些参数,但是就我而言,我为该User模型提供了一个额外的username属性,因此我将其添加到ApplicationController中:

  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected
  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:account_update) << :username
    devise_parameter_sanitizer.for(:sign_up) << :username
  end

add following in your ApplicationController (if u have Rails4) 在您的ApplicationController中添加以下内容(如果您有Rails4)

before_filter :update_sanitized_params, if: :devise_controller?

#  method to sanitized params for devise user sign up
def update_sanitized_params
    devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:email,:password, :password_confirmation)}
end

for more info see https://github.com/plataformatec/devise#getting-started "Strong Parameters" section 有关更多信息,请参见https://github.com/plataformatec/devise#getting-started的 “严格参数”部分

If anyone else is getting this error still, check your form url path. 如果还有其他人仍然出现此错误,请检查您的表单URL路径。 I had copied the session_path into the form that should have used registration_path. 我已经将session_path复制到应该使用registration_path的表单中。

I'm doing my first use of Devise GEM (currently learning rails) with this exercise. 我正在本练习中首次使用Devise GEM(目前正在学习Rails)。 In my case, I had the same error and it was a routing problem. 就我而言,我有同样的错误,这是一个路由问题。

The issue was on a form_for in the new.index.html.erb of the devise/registrations views folder . 问题出在new.index.html.erb devise/registrations视图文件夹的new.index.html.erb中的form_for上。

The form had a url: session_path(resource_name) , but in order to work it should point to registration_path(resource_name) . 该表单具有url: session_path(resource_name) ,但是为了起作用,它应该指向registration_path(resource_name)

I changed that and now it is working on heroku. 我改变了它,现在它在heroku上工作了。

This is a link to the file. 是文件的链接。

And this is the exercise live on heroku: devise.muga.com.co 这是heroku上的练习: devise.muga.com.co

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM