【问题标题】:Editing custom fields in Devise User model在设计用户模型中编辑自定义字段
【发布时间】:2015-07-10 06:41:15
【问题描述】:

这些字段是通过迁移添加的,并且视图的表单也已创建,但控制器会在其从视图到模型的路径上过滤参数。无论我做什么,我的参数总是不允许的。我的控制器代码

#app/controllers/users/registrations_controller.rb

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

  protected

  # If you have extra params to permit, append them to the sanitizer.
  def configure_sign_up_params
   devise_parameter_sanitizer.for(:sign_up)<<[:first_name,:last_name,:profile_image,:graduation_year]
  end

  # If you have extra params to permit, append them to the sanitizer.
  def configure_account_update_params
   devise_parameter_sanitizer.for(:account_update)<<[:first_name,:last_name,:profile_image,:graduation_year]
  end

end
end

#config/routes.rb

Rails.application.routes.draw do
#...

  devise_for :users, controllers: { account_update: "users/registrations", sign_up:"users/registrations" }
end

#错误

Parameters: {"utf8"=>"✓", "authenticity_token"=>"Qts15L3n6Xvsn0hwNvIUI6UrWUQyV/qEyoQAZ8M+udMK1RBTQS1XoNWgpg1JrXqWpb9NbrsaHtQVVU8XMwoSIQ==", 
 "user"=>{"first_name"=>"a", "last_name"=>"a", 
 "profile_image"=>#<ActionDispatch::Http::UploadedFile:0x00000004fe0bb0 @tempfile=#<Tempfile:/tmp/RackMultipart20150709-4420-12guerh.jpeg>, @original_filename="test1.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[profile_image]\"; filename=\"test1.jpeg\"\r\nContent-Type: image/jpeg\r\n">, 
 "graduation_year"=>"1", "email"=>"aaaaaa@a.a", 
 "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, 
 "commit"=>"Submit"}

Unpermitted parameters: first_name, last_name, profile_image, graduation_year

感谢大家的帮助。真的很感激!

【问题讨论】:

  • 如果有效,请接受我的回答:)
  • 对不起队友,但错误最终出现在路线文件中,我在下面分享了我的答案。 configure_params 方法的原始语法可以正常工作,尽管您的语法更简洁。

标签: ruby-on-rails ruby-on-rails-4 web devise strong-parameters


【解决方案1】:

我的 config/routes.rb 搞砸了。它必须是

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

然后我需要将 :email, :password, :password_confirmation 添加回 app/controllers/users/registrations_controller.rb

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

  def configure_sign_up_params
    devise_parameter_sanitizer.for(:sign_up)<<[:first_name,:last_name,:profile_image,:graduation_year,
       :email,:password,:password_confirmation]
  end

  def configure_account_update_params
    devise_parameter_sanitizer.for(:account_update)<<[:first_name,:last_name,:profile_image,:graduation_year,
       :email,:password,:password_confirmation]
  end
end

文件底部还有一个额外的“结尾”。

更新

在当前版本的 devise (4.3)/rails (5.1.3) 中是类似的,但配置函数应该更新为这样的:

def configure_sign_up_params
  devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, :age, :height, :weight, :gender])
end

【讨论】:

  • 要使用 configure_sign_up_params,您还需要取消注释控制器顶部的一行,以便调用它。
【解决方案2】:

我遇到了同样的问题,像下面这样的改变对我有用。

def configure_sign_up_params
   devise_parameter_sanitizer.for(:sign_up) { |u| u.permit( :first_name, :last_name, :profile_image, :graduation_year) }
end

def configure_account_update_params
   devise_parameter_sanitizer.for(:account_update) { |u| u.permit( :first_name, :last_name, :profile_image, :graduation_year) }
end

【讨论】:

  • 谢谢伙计,我会尽快检查并报告
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-19
  • 1970-01-01
  • 2011-05-05
  • 1970-01-01
  • 2015-02-25
  • 1970-01-01
  • 2021-04-15
相关资源
最近更新 更多