未经允许的参数添加新的领域来devise在轨道4.0

非常新的使用轨道。 我已经使用Devise实现了一个基本的login系统。 我试图在sign_up页面添加一些新的字段(bio:string,name:string)。 我有一切正确显示,并且新的字段被添加到数据库(当我在SQLbrowser中查看它),但是,他们不填充,并且在用户提交sign_up表单后,有一个消息的哪部分说:

Unpermitted parameters: bio, name 

我已经添加了2个string到_devise_create_users.rb

  # added t.string :bio t.string :name 

我有他们出现在schema.rb

 ActiveRecord::Schema.define(version: 20130629002343) do create_table "users", force: true do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.integer "sign_in_count", default: 0 t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.datetime "created_at" t.datetime "updated_at" t.string "shortbio" t.boolean "admin", default: false t.string "realname" t.string "name" t.string "bio" end add_index "users", ["email"], name: "index_users_on_email", unique: true add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end 

我的user.rb

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

这个问题与强参数有关吗? 我正在困难的时间,在他们周围,在哪里/如何执行。

被接受的解决scheme是足够好的,但是我看到两个问题:1)所有的控制器将检查当前的控制器是否是devise控制器( if: :devise_controller? )和2)我们需要在方法中写入所有可接受的参数...for(:sign_up) {|u| u.permit(:bio, :name)} ),甚至是:email ...for(:sign_up) {|u| u.permit(:bio, :name)} :password等等。

我认为更优雅的解决scheme可能是:

 # app/controllers/users/registrations_controller.rb class Users::RegistrationsController < Devise::RegistrationsController before_filter :configure_permitted_parameters protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up).push(:name, :phone, :organization) end end # config/routes.rb devise_for :users, :controllers => { :registrations => "users/registrations" } 

注:更新为Rails 4.2 +

这个答案已经过时了:

  • 在Rails 4.2.1和Devise 3.4.1的“用户/注册”path中将“users”更改为“user”。
  • devise_parameter_sanitizer.permit()replaceDevise 4的devise_parameter_sanitizer.for() (参见Rails 5,未定义的方法for'for#<devise on line devise_parameter_sanitizer.for )

确保至less使用Devise 3.0.0。 添加到您的应用程序控制器

 before_filter :update_sanitized_params, if: :devise_controller? def update_sanitized_params devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:bio, :name)} end 

文档: https : //github.com/plataformatec/devise#strong-parameters

我也遇到了麻烦。 devise网站上的文档以及一些论坛帮助。 这就是我最终做的事情:

在自定义的RegistrationsController(app / controllers / users / registrations_controller.rb)

 # app/controllers/users/registrations_controller.rb class Users::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, :password, :password_confirmation)} end end 

然后在你的path文件(config / routes.rb)中为你的devise_for声明:

 devise_for :users, controllers: {registrations: "users/registrations"} 

这是另一个直接的方式,在我的轨道4.2.1应用程序:

创build以下文件

 /config/initializers/devise_permitted_parameters.rb 

和代码..

 module DevisePermittedParameters extend ActiveSupport::Concern included do before_filter :configure_permitted_parameters end protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) << :name devise_parameter_sanitizer.for(:account_update) << :name devise_parameter_sanitizer.for(:sign_up) << :bio devise_parameter_sanitizer.for(:account_update) << :bio end end DeviseController.send :include, DevisePermittedParameters 

对于sign_up和account_update,都要为controllers/applcation_controller.rb applcation_controller.rb执行此操作

 class ApplicationController < ActionController::Base protect_from_forgery with: :exception before_action :authenticate_user! before_action :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:password, :password_confirmation,:current_password,:email,:name, :phonenumber,:province,:city,:area,:idcardimg,:role) } devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:password, :password_confirmation,:current_password,:email,:name, :phonenumber,:province,:city,:area,:idcardimg,:role) } end end 

问题似乎与强大的参数,看看这里,并复制代码。

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

将该文件复制到项目app/controllers/devise/registrations_controller.rb的相同位置

并更改创build操作的代码

 # POST /resource def create # THIS LINE IS THE ONE YOU CHANGE self.resource = build_resource(sign_up_params.merge(:bio, :name)) 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 

我必须告诉你,Iam不太确定这是否可行,因为我不使用devise,但看到它的代码似乎会起作用。

为此devise一切:

在你有用户控制器

 private # Never trust parameters from the scary internet, only allow the white list through. def user_params params.require(:user).permit(:full_name <add your parameter>) end