0

我对rails比较陌生,所以在回答时请记住这一点。:)

我已将clearancegem 添加到我的 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)

更新:我在成功保存新用户后通过更改重定向 url 修复了之前的错误。我还必须将以下内容添加到我的UsersController

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

有任何想法吗?

4

1 回答 1

0

注册后仍然无法让它自动登录。相反,我在内部实施了一种解决方法Users#create

if @user.save
  format.html { redirect_to sign_in_path, notice: "Welcome, #{@user.first_name}!\nSign in to continue." }

  ...

end
于 2015-10-19T00:09:55.913 回答