53

使用 Rails 非常新。我已经使用 Devise 实现了一个基本的登录系统。我正在尝试将几个新字段(bio:string,name:string)添加到 sign_up 页面中。我的所有内容都正确显示,并且新字段已添加到数据库中(当我在 SQLbrowser 中查看时),但是它们没有填充,并且在用户提交注册表单后,会显示一条消息,其中一部分显示:

Unpermitted parameters: bio, name

我已将 2 个字符串添加到 _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

我的用户.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

这个问题与强参数有关吗?我很难理解它们以及在哪里/如何实施。

4

7 回答 7

71

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

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

# 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+ 的更新

这个答案已经过时了:

于 2013-09-26T18:57:45.903 回答
40

确保您至少使用的是 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

于 2013-06-29T20:32:49.087 回答
11

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

在自定义 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

然后在您的路由文件 (config/routes.rb) 中为您的 devise_for 语句使用这个:

devise_for :users, controllers: {registrations: "users/registrations"}
于 2013-07-04T04:19:35.273 回答
4

这是在我的 rails 4.2.1 应用程序中工作的另一种直接方式:

创建以下文件

/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
于 2015-08-18T00:14:56.503 回答
2

对于 sign_up 和 account_update 都这样做controllers/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
于 2015-06-27T08:24:11.663 回答
0

设计为此准备了一切:

在用户控制器中,您拥有

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
于 2014-01-23T13:26:48.800 回答
0

问题似乎与强参数有关,请查看此处并复制代码。

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

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

并更改创建操作的代码

# 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

我必须告诉你,我不太确定这是否有效,因为我不使用设计,但看到代码似乎它会起作用。

于 2013-06-29T20:29:34.227 回答