0

我有这个控制器。

class SessionsController < Devise::SessionsController
  # GET /resource/sign_in
  def new
    self.resource = build_resource(nil, :unsafe => true)
    clean_up_passwords(resource)
    respond_with(resource, serialize_options(resource))
  end

  # POST /resource/sign_in
  def create
    self.resource = warden.authenticate!(auth_options)
    set_flash_message(:notice, :signed_in, :username => resource.username) if is_navigational_format?
    sign_in(resource_name, resource)
   respond_with resource, :location => after_sign_in_path_for(resource)
 end
end

黄瓜的这个步骤定义:

Given /^a user "(.*?)" exists$/ do |user_name|
    @user = User.create!(:username => user_name , :password => "tamara")
end

When /^he logs in$/ do 
  visit("/users/sign_in")
  fill_in('Username', :with => "roelof")
  fill_in('Password', :with => "tamara")
  click_button('Sign in')
end

Then /^he should see "(.*?)"$/ do |message|
  page.should have_content(message)
end

只有在成功登录后一切正常,我才重定向到主页而不是登录成功页面。所以我没有看到闪存消息。

罗洛夫

编辑 1:我检查了控制器和资源名称,资源似乎具有正确的值。

4

2 回答 2

0

after_sign_in_path_for 的标准 DeviseHelper 是 signed_in_root_path

  # The scope root url to be used when he's signed in. By default, it first
  # tries to find a resource_root_path, otherwise it uses the root_path.

您可以检查您的路线和范围,甚至可以通过使用调试器行将 Devise signed_in_root_path 复制到您的 ApplicationController 中进行调试

于 2012-11-09T16:51:36.850 回答
0

默认情况下,Devise 的内部after_sign_in_path_for(resource)方法首先尝试在会话中找到有效的 {resource}_return_to 键,然后回退到 {resource}_root_path,否则它使用您的根路径路由。

例如,user_root_path成功登录后设置将在用户范围内设置直接路径(可能是您想要的):

# config/routes.rb
...
devise_for :users do
 get 'users', :to => 'users#show', :as => :user_root 
end

甚至:

# config/routes.rb
...
match 'user_root' => 'users#show'

通过覆盖来覆盖成功登录时的重定向的另一种方法after_sign_in_path_for(resource)

# app/controllers/application_controller.rb
...
def after_sign_in_path_for(resource)
  # Put some path, like:
  current_user_path
end

供您阅读的链接:

于 2012-11-10T09:23:53.353 回答