Ruby on rails 无法将密码\u重置\u令牌保存到数据库
我在测试中收到一个错误,尽管代码正常工作。对于实例方法中的属性集,Capybara抛出未定义的方法错误是否正常?我的代码如下: app/models/user.rb 水豚错误信息 当您有一个不想保存到数据库中的值时,应该使用attr_accessor。在这种情况下,您不需要它,因为您希望将:password\u reset\u令牌保存到db中,rails已经提供了setter和getter方法 编辑 使用attr_访问器更改getter和setter方法并定义实例变量。它不会更改活动记录使用的属性。这意味着在保存模型时,即使存在相关列,使用attr_accessor设置的值也不会保存到数据库中 打印属性时,您可以看到我的意思:Ruby on rails 无法将密码\u重置\u令牌保存到数据库,ruby-on-rails,ruby,Ruby On Rails,Ruby,我在测试中收到一个错误,尽管代码正常工作。对于实例方法中的属性集,Capybara抛出未定义的方法错误是否正常?我的代码如下: app/models/user.rb 水豚错误信息 当您有一个不想保存到数据库中的值时,应该使用attr_accessor。在这种情况下,您不需要它,因为您希望将:password\u reset\u令牌保存到db中,rails已经提供了setter和getter方法 编辑 使用attr_访问器更改getter和setter方法并定义实例变量。它不会更改活动记录使用的属
u = User.new
u.password_reset_token = "abc"
u.attributes #=> { ... "password_reset_token"=>nil ... }
在同一个属性上同时使用attr\u accessor和attr\u accessible是没有意义的。您应该从模型中删除attr\u accessor:password\u reset\u令牌
这可能不能完全解决问题,但它应该能帮助你找出真正的问题所在
我建议您为Userset\u password\u reset\u令牌编写一个单元测试,以确保它在编写验收测试之前的行为符合您的预期。嘿,Benjamin,谢谢您的回答,但问题不在于实际的密码。密码重置令牌(应该是16位随机字符串)不会保存到数据库中。即使当我在控制台中执行self.password\u reset\u token=abc之类的操作时,reset\u token也不会保存,即使它说它是有效的并说保存!是的。另外,我使用attr_访问器的原因是因为在set_password_reset方法中执行self.password_reset_token=self.class.generate_token时得到了一个NoMethodError,password_reset_token。因此,我删除了attr_访问器,现在我可以将列保存到数据库中,但是,在我的集成测试中,我仍然得到一个关于setter方法未定义的错误。但是,密码重置确实有效。请将密码重置添加到属性可访问列表中以允许您设置此值。我忘记添加在属性可访问列表中的编辑。。。你认为这是一个来自水豚的虫子,因为它实际上工作正常吗?再次感谢你的帮助本杰明。哎呀,它就在那里,但是一直贴到右边。你能把水豚的错误/失败消息包括进来吗?
class PasswordResetsController < ApplicationController
skip_before_filter :require_current_user!
def new
end
def create
user = User.find_by_email(params[:email])
user.send_password_reset if user
flash[:notice] = "Email sent with password reset instructions."
redirect_to signin_url
end
def edit
@user = User.find_by_password_reset_token(params[:id])
end
def update
@user = User.find_by_password_reset_token(params[:id])
if @user.update_attributes(params[:user])
flash[:notice] = "Password has been reset!"
redirect_to signin_url
else
render :edit
end
end
end
create_table "users", :force => true do |t|
t.string "email", :null => false
t.string "password_digest", :null => false
t.string "first_name", :null => false
t.string "last_name", :null => false
t.string "session_token", :null => false
t.string "bio"
t.string "location"
t.string "focus"
t.string "aspirations"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "profile_photo_file_name"
t.string "profile_photo_content_type"
t.integer "profile_photo_file_size"
t.datetime "profile_photo_updated_at"
t.string "resume_file_name"
t.string "resume_content_type"
t.integer "resume_file_size"
t.datetime "resume_updated_at"
t.string "school"
t.string "password_reset_token"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["session_token"], :name => "index_users_on_session_token", :unique => true
PasswordResets
updates the user password when confirmation matches (FAILED - 1)
Failures:
1) PasswordResets updates the user password when confirmation matches
Failure/Error: user.set_password_reset_token
NoMethodError:
undefined method `password_reset_token=' for #<User:0x007fd237d31e20>
# ./app/models/user.rb:107:in `set_password_reset_token'
# ./spec/features/password_resets_spec.rb:25:in `block (2 levels) in <top (required)>'
Finished in 0.19426 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/features/password_resets_spec.rb:23 # PasswordResets updates the user password when confirmation matches
u = User.new
u.password_reset_token = "abc"
u.attributes #=> { ... "password_reset_token"=>nil ... }