【发布时间】:2013-10-01 12:50:27
【问题描述】:
我已经尝试了我能找到的所有内容,包括 Devise README (https://github.com/plataformatec/devise#strong-parameters) 中的内容、对 README (http://blog.12spokes.com/web-design-development/adding-custom-fields-to-your-devise-user-model-in-rails-4/comment-page-1/#comment-26217) 的更具描述性的内容,并且我在 StackOverflow 上找到的每个答案似乎都与这两个一致解决方案。所有这些解决方案都会导致相同的错误: 2 个错误导致此用户无法保存: - 电子邮件不能为空 - 密码不能为空
我还尝试在 gemfile 和运行包中包含 gem 'protected_attributes',以便我可以使用 attr_accessible 而不是强参数。通过这种方法,我似乎能够很好地保存用户,但我的任何字段,无论是自定义的还是其他的,实际上都没有保留。他们都保持为零。我还尝试将设计恢复到使用 attr_accessible 而不是强参数的先前版本,这导致了同样的问题,没有任何东西持续存在。
我目前对此完全停滞不前,因此对于其他可能有效的解决方案的任何想法将不胜感激......提前致谢!
编辑:这是我的应用程序控制器:
class ApplicationController < ActionController::Base
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:profile_name, :email, :password) }
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :profile_name, :email, :password, :password_confirmation) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :profile_name, :email, :password, :password_confirmation, :current_password) }
end
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
end
【问题讨论】:
-
你的用户控制器是什么样的?你在控制器中添加了强参数的方法吗?
-
我正在通过我的应用程序控制器将强参数传递给我的用户模型。我将我的代码添加到我原来的问题中。谢谢你的帮助!如果您需要任何其他信息,请告诉我。
-
本教程帮助我解决了这些问题。如果它也对其他人有帮助:jacopretorius.net/2014/03/…
标签: ruby-on-rails ruby devise ruby-on-rails-4 attr-accessible