简体   繁体   English

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

[英]Rails 4 + Devise 3 - How to permit parameters for update action?

In the users controller: 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

I want to allow users update their name + email without being asked to fill out their password, so this is the controller for doing the update: 我想允许用户更新其姓名+电子邮件,而无需要求他们填写密码,因此这是进行更新的控制器:

class RegistrationsController < Devise::RegistrationsController

  protected

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

It's configured in routes.rb . 它在routes.rb配置。

But when I try to update the attributes, I get this message: 但是,当我尝试更新属性时,出现以下消息:

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

How to make another parameters/attributes accessible? 如何使其他参数/​​属性可访问?

In user.rb is only the following: 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

Thank you guys in advance. 预先谢谢你们。

Have you whitelisted your parameters?. 您是否已将参数列入白名单? In applicationController : applicationController

before_filter :configure_permitted_parameters, if: :devise_controller?

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

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

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