5

设计不会重定向到由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

正在被调用,它给出了正确的 url,str完全符合我的预期。调试器此时停止...

但是在cont页面停留在之后sign_in,却发生了登录。

我相信这不是我的代码问题。可能是设计问题。任何可以使用它的人都可以我分享与您一起使用的 Devise 的确切版本。

4

1 回答 1

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

respond_with方法将根据资源的状态明智地采取行动。由于它再次呈现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)

你看到那个重定向了吗?

于 2012-10-18T17:12:01.513 回答