我正在为商店设计一个结账页面。当表单被提交时,这个行为会在我的控制器中执行。
def update_billing
...
if @checkout.save
sign_in(guest_user) #<--
redirect_to root_path, notice: "Success!"
else
render 'billing'
end
end
我通过在操作中使用raise
并使用better_errors进行测试来验证此项:
>> sign_in(guest_user) if params[:checkout_form][:create_an_account] == "1"
=> #<User id: 8, email: "abc123@yahoo.com", encrypted_password: "$2a$10$HCv7veSO7LC9Dh1tKD0Jbe57Pz6lAsiZgfIiWOys7bF...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 6, current_sign_in_at: "2014-06-04 19:45:36", last_sign_in_at: "2014-06-04 19:29:48", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2014-06-04 18:50:29", updated_at: "2014-06-04 19:45:36", guest: false, guest_email: "guest_140190782975@example.com">
>> current_user
=> #<User id: 8, email: "abc123@yahoo.com", encrypted_password: "$2a$10$HCv7veSO7LC9Dh1tKD0Jbe57Pz6lAsiZgfIiWOys7bF...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 6, current_sign_in_at: "2014-06-04 19:45:36", last_sign_in_at: "2014-06-04 19:29:48", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2014-06-04 18:50:29", updated_at: "2014-06-04 19:45:36", guest: false, guest_email: "guest_140190782975@example.com">
>> session["warden.user.user.key"]
=> [[8], "$2a$10$HCv7veSO7LC9Dh1tKD0Jbe"]
但是,当我返回到localhost:3000/时,current_user
再次变成nil
:
>> session["warden.user.user.key"]
=> nil
>> current_user
=> nil
sign_in(guest)方法不会设置会话变量,以便current_user
在请求之间"保持"。
我做错了什么?
@checkout.save
表单对象中,我更新了guest_user
的属性并保存了它。我认为 Devise 必须登录最新版本的guest_user
,所以在sign_in(guest_user)
之前,我不得不添加一个guest_user.reload
。 - bigpotatoreload
方法来更新用户信息。 - bigpotato