设计after_sign_in_path_for有效,但重定向不会发生

时间:2012-10-18 16:22:22

标签: ruby-on-rails devise

Devise不会重定向到after_sign_in_path_for提供的网址。 它实际上调用了我的自定义after_sign_in_path_for。它会计算我希望它计算的url,但之后不会发生重定向。它保留在sign_in页面,但实际登录不会发生。

我一直在尝试几个版本的设计:

gem 'devise'

gem 'devise', :git => 'git://github.com/plataformatec/devise.git'

同样的结果。

我的自定义after_sign_in_path_for

def after_sign_in_path_for(resource)
    str = stored_location_for(resource) || stored_location || root_path
    debugger
    str
end

def stored_location
    session.delete(:return_to)
end

def store_location
    session[:return_to] = request.fullpath
end

正在被调用,它会提供正确的网址,正如我预期的那样提供str。 调试器此时停止...

但在cont页面保持sign_in之后,然而登录就会发生。

我认为这不是我的代码问题。这可能是一个设计问题。任何拥有工作的人都可以与我分享Devise的确切版本,它适合您。

1 个答案:

答案 0 :(得分:2)

很难说没有看到您的服务器日志。如果正在调用after_sign_in_path_for并生成预期的URL,则会留下一种可能性:

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

The respond_with method will act sensibly according to the status of the resource。由于它再次呈现new模板,因此它指向某种验证错误或其他问题登录。

查看服务器日志。以下是使用我的应用中的after_sign_in_path_for成功登录和后续重定向:

Started POST "/admins/sign_in" for 127.0.0.1 at 2012-10-18 09:59:27 -0700
[INFO] [127.0.0.1] [2012-10-18 09:59:27 -0700] Processing by Devise::SessionsController#create as HTML
[INFO] [127.0.0.1] [2012-10-18 09:59:27 -0700]   Parameters: {"utf8"=>"✓", "authenticity_token"=>"DGjs2b3k8BIi62KWCn3u5kx7YxxyR03xkERcgH/ilr0=", "admin"=>{"email"=>"me@mydomain.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
[DEBUG] [127.0.0.1] [2012-10-18 09:59:27 -0700]   Admin Load (2.1ms)  SELECT "admins".* FROM "admins" WHERE "admins"."email" = 'me@mydomain.com' LIMIT 1
[DEBUG] [127.0.0.1] [2012-10-18 09:59:27 -0700]    (1.0ms)  BEGIN
[DEBUG] [127.0.0.1] [2012-10-18 09:59:27 -0700]    (0.9ms)  UPDATE "admins" SET "last_sign_in_at" = '2012-10-18 16:53:19.428068', "current_sign_in_at" = '2012-10-18 16:59:28.076180', "last_sign_in_ip" = '127.0.0.1', "sign_in_count" = 3, "updated_at" = '2012-10-18 16:59:28.078677' WHERE "admins"."id" = 1
[DEBUG] [127.0.0.1] [2012-10-18 09:59:27 -0700]    (9.3ms)  COMMIT
[INFO] [127.0.0.1] [2012-10-18 09:59:27 -0700] Redirected to http://localhost:3001/admin/users
[INFO] [127.0.0.1] [2012-10-18 09:59:27 -0700] Completed 302 Found in 97ms (ActiveRecord: 0.0ms)
[INFO] [127.0.0.1] [2012-10-18 09:59:28 -0700] 

Started GET "/admin/users" for 127.0.0.1 at 2012-10-18 09:59:28 -0700
[INFO] [127.0.0.1] [2012-10-18 09:59:28 -0700] Processing by UsersController#index as HTML
[DEBUG] [127.0.0.1] [2012-10-18 09:59:28 -0700]   Admin Load (1.2ms)  SELECT "admins".* FROM "admins" WHERE "admins"."id" = 1 LIMIT 1
[DEBUG] [127.0.0.1] [2012-10-18 09:59:28 -0700]   User Load (1.0ms)  SELECT "users".* FROM "users" 
[INFO] [127.0.0.1] [2012-10-18 09:59:28 -0700]   Rendered users/index.html.erb within layouts/application (1.0ms)
[INFO] [127.0.0.1] [2012-10-18 09:59:28 -0700] Completed 200 OK in 22ms (Views: 16.5ms | ActiveRecord: 2.2ms)

您是否看到了重定向?