使用Devise 3.2.4
和Rails 4.1.1
,除了Devise
和email
之外,我无法更新password
与用户相关的任何内容。
我的edit.html.erb
中包含以下内容(除默认设置外):
<%= f.collection_select :plan_id, Plan.all, :id, :name if current_user.admin? %>
以及我的application_controller.rb
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :configure_permitted_paramaters, if: :devise_controller?
protected
# let devise accept more paramaters
def configure_permitted_paramaters
devise_parameter_sanitizer.for(:account_update) do |u|
u.permit(:email, :password, :password_confirmation,
:plan_id, :current_password)
end
devise_parameter_sanitizer.for(:sign_up) do |u|
u.permit(:first_name, :last_name, :employee_number,
:email, :password, :password_confirmation)
end
end
end
然而,当我提交表单时,我得到了正确的参数(包括更改的plan_id
):
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Token",
"user"=>{"email"=>"taylor@taylorskidmore.com", "password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]", "plan_id"=>"5"},
"commit"=>"Update"}
但除了新密码之外,实际数据库中没有任何变化。有没有人有任何想法?
以下是put的整个服务器日志:
Started PUT "/users" for 127.0.0.1 at 2014-06-11 11:30:09 -0400
Processing by Users::RegistrationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Token", "user"=>
{"email"=>"taylor@taylorskidmore.com", "password"=>"[FILTERED]", "password_confirmation"=>
"[FILTERED]", "current_password"=>"[FILTERED]", "plan_id"=>"1"}, "commit"=>"Update"}
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
(0.1ms) BEGIN
Division Load (0.2ms) SELECT `divisions`.* FROM `divisions` WHERE `divisions`.`name` = 'Sample Name' LIMIT 1
Plan Load (0.2ms) SELECT `plans`.* FROM `plans` WHERE `plans`.`company` = 'company 1' AND `plans`.`shortname` = 'plan 1' LIMIT 1
(0.1ms) COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 106ms (ActiveRecord: 2.6ms)
我的user.rb
:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
validate :user_is_eligible
belongs_to :plan
belongs_to :division
def is_eligible?
db = Sequel.connect('mysql2://localhost')
eligible_users = db[:eligible_users]
user = eligible_users.where(:first_name => self.first_name, :last_name => self.last_name,
:employee_number => self.employee_number)[:id]
if user.present?
@division = Division.find_by_name(user[:division])
self.division_id = @division.id
self.plan_id = Plan.find_by_company_and_shortname(@division.company, user[:plan].downcase).id
end
user.present?
end
private
def user_is_eligible
errors.add(:base, "Check Employee Information and Try Again") unless self.is_eligible?
end
end
答案 0 :(得分:1)
您在User
模型中进行了验证,您正在重置plan_id
,这就是您从表单传递的plan_id
未在数据库中更新的原因。< / p>
根据聊天讨论,您只需在创建用户记录时进行验证,以便您可以执行以下验证选项on: :create
,以便在更新现有内容时不会执行验证用户记录。
class User < ActiveRecord::Base
#..
validate :user_is_eligible, on: :create ## Updated this line
## ...
end