'注册'流程失败

时间:2015-10-05 03:09:26

标签: ruby-on-rails-4 authentication clearance

我对rails很新,所以在回答时请记住这一点。 :)

我已将clearance gem添加到我的rails应用中。我执行了rails g clearance:views,没有麻烦。接下来我执行了rails g clearance:specs。除了一个之外,所有的间隙规格都通过了:

/spec/features/clearance/visitor_signs_up_spec.rb:13 # Visitor signs up with valid email and password

这里是rspec错误消息:

Visitor signs up with valid email and password Failure/Error: expect_user_to_be_signed_in expected to find button "Sign out" but there were no matches # ./spec/support/features/clearance_helpers.rb:35:in `expect_user_to_be_signed_in' # ./spec/features/clearance/visitor_signs_up_spec.rb:16:in `block (2 levels) in <top (required)>' # -e:1:in `<main>'

有趣的是,当我尝试手动关注规范时,注册过程完成,但它不会创建新会话。换句话说,我必须在注册后登录。根据该规范,这似乎与/spec/features/clearance/visitor_signs_up_spec.rb:13中出现的行为相符,尽管它不是预期的行为。

使用相同方法expect_user_to_be_signed_in的其他规范都没有这个问题。

此外,我的rails服务器日志并未指出任何错误。

我不知道还有什么方法可以缩小问题范围。你觉得怎么样?

更新:发现错误!

Started POST "/users" for ::1 at 2015-10-18 15:15:44 -0600
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "user"=>{"first_name"=>"Michael", "last_name"=>"Hargiss", "username"=>"mycargus", "email"=>"valid@example.com", "password"=>"[FILTERED]"}, "commit"=>"Sign up"}
Redirected to http://localhost:3000/sign_in
Filter chain halted as :require_login rendered or redirected
Completed 302 Found in 1ms (ActiveRecord: 0.0ms)

更新:我通过在成功保存新用户后更改重定向网址来修复上一个错误。我还必须将以下内容添加到我的UsersController

before_action :require_login, only: [:index, :show, :edit, :update, :destroy]

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

注册后仍然无法自动登录。相反,我在Users#create

中实施了一种解决方法
if @user.save
  format.html { redirect_to sign_in_path, notice: "Welcome, #{@user.first_name}!\nSign in to continue." }

  ...

end