Rails 4 + Devise 3 - 如何允许参数进行更新操作?

时间:2015-02-21 10:32:09

标签: ruby-on-rails ruby devise attributes updatemodel

users控制器中:

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]
  ...
  private
    def set_user
      @user = User.find(params[:id])
    end

    def user_params
      accessible = [ :name, :name_slug, :email, :avatar_url ] # extend with your own params
      accessible << [ :password, :password_confirmation ] unless params[:user][:password].blank?
      params.require(:user).permit(accessible)
    end
end

我想允许用户更新他们的姓名+电子邮件,而不会被要求填写他们的密码,因此这是进行更新的控制器:

class RegistrationsController < Devise::RegistrationsController

  protected

  def update_resource(resource, params)
    puts 'params'
    puts params.inspect
    resource.update_without_password(params)
  end
end

已在routes.rb中配置。

但是当我尝试更新属性时,我收到了这条消息:

Unpermitted parameters: name
params
{"email"=>"my_email@gmail.com"}

如何使其他参数/​​属性可访问?

user.rb中只有以下内容:

class User < ActiveRecord::Base
  TEMP_EMAIL_PREFIX = 'change@me'
  TEMP_EMAIL_REGEX = /\Achange@me/

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

  validates_format_of :email, :without => TEMP_EMAIL_REGEX, on: :update
end

提前谢谢你们。

1 个答案:

答案 0 :(得分:1)

您是否已将参数列入白名单?在applicationController

before_filter :configure_permitted_parameters, if: :devise_controller?

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:account_update) << [:name]
end