我正在办理商店结账。提交表单后,此操作将在我的控制器中进行
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
跨请求“持久”?
我做错了什么?
答案 0 :(得分:3)
我明白了。在我的@checkout
表单对象中,我更新guest_user
s属性并在调用@checkout.save
时保存它。我认为设计必须在最新版本的guest_user中登录,因此我必须在guest_user.reload
之前添加sign_in(guest_user)
简而言之,请在签名之前重新加载您的用户(user.reload
)。
答案 1 :(得分:0)
+1个要添加的内容。初始化程序/设计中有一个设置,可能会导致sign_in意外行为:
config.skip_session_storage
如果您尝试在配置为跳过的操作内登录,则将无法执行此操作。