如何让 Devise 3.4 允许参数 Rails 4.2

How do I get Devise 3.4 to permit Parameters Rails 4.2

欢迎就如何改进这个问题提出建议

生成Devise用户后,我添加了3个东西。

t.integer  "role"
t.string   "firstname"
t.string   "lastname"

在用户注册时允许使用这些参数并正确创建用户。

当用户尝试编辑他们的帐户时,"firstname" 和 "lastname" 值可以正常更改,但是当用户尝试在他们的 /users/edit 页面上更改他们的角色时,没有错误已给出,flash 显示 "account updated successfully" 但角色值未更改。

来自 /log/development.log 显示所有 3 个参数都是不允许的,如果真的是这样我不知道为什么其他两个可以更新。

Parameters: {"utf8"=>"✓", "authenticity_token"=>"LnVPFFJKV+RtnB21ZUGr4HF1siVcEuT/BRXaLVkch1nWQXiGRFVGhdWchlQSZ9A7mFgKX2njEjCbqR4CHp5hmQ==", "user"=>{"role"=>"worker", "firstname"=>"asdfDe Wet", "lastname"=>"Blomerus", "email"=>"dewet@blomerus.org", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]"}, "commit"=>"Update"}
  [1m[36mUser Load (0.8ms)[0m  [1mSELECT  "users".* FROM "users" WHERE "users"."id" =   ORDER BY "users"."id" ASC LIMIT 1[0m  [["id", 6]]
  [1m[35mUser Load (0.4ms)[0m  SELECT  "users".* FROM "users" WHERE "users"."id" =  LIMIT 1  [["id", 6]]
Unpermitted parameters: role, firstname, lastname
Redirected to http://localhost:3000/
Completed 302 Found in 84ms (ActiveRecord: 1.5ms)

/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) << [:firstname, :lastname, :role]
    devise_parameter_sanitizer.for(:account_update) << [:firstname, :lastname, :role]
  end

end

DeviseController.send :include, DevisePermittedParameters

/app/controllers/users_controller.rb

的相关部分
def update
  @user = User.find(params[:id])
  if @user.update_attributes(secure_params)
    redirect_to users_path, :notice => "User updated."
  else
    redirect_to users_path, :alert => "Unable to update user."
  end
end

private

def secure_params
  params.require(:user).permit(:role, :firstname, :lastname)
end

更新操作永远不会运行,我可以完全注释掉它并且没有任何变化。

这就是对我有用的设计:

我改users/registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController
  before_action :configure_permitted_parameters, only: [:create]
  before_filter :configure_account_update_params, only: [:update]

  def create
   super
  end

  # GET /resource/edit
  def edit
    super
  end

  # PUT /resource
  def update
    super
  end

  # DELETE /resource
   def destroy
       super
   end

  protected

def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) do |u|
      u.permit(:first_name, :last_name, :user_name, :email, :password, :password_confirmation, :avatar, :avatar_cache)
    end
end

def configure_account_update_params
    devise_parameter_sanitizer.for(:account_update)do |u|
      u.permit(:first_name, :last_name, :user_name, :email, :password, :password_confirmation, :current_password, :avatar, :avatar_cache)
  end
end

我没有在 users_controller.rb 中定义任何更新操作。不需要。另外,我不使用您正在定义的任何类型的模块,它工作正常。