2011-11-22 35 views
13

我需要设计认证gem的路由问题帮助,在成功登录后重定向到自定义页面,以便通过输入测试人员名称来创建新记录和年龄(测试数据)设计after_sign_in_path_for ...发送到.... root_path - 查询

我使用的Rails 3色器件版本1.4.9

我的航线如下

new_user_session GET /users/sign_in(.:format)  {:action=>"new", :controller=>"devise/sessions"} 
     user_session POST /users/sign_in(.:format)  {:action=>"create", :controller=>"devise/sessions"} 
destroy_user_session DELETE /users/sign_out(.:format)  {:action=>"destroy", :controller=>"devise/sessions"} 
     user_password POST /users/password(.:format)  {:action=>"create", :controller=>"devise/passwords"} 
    new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"} 
    edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"} 
        PUT /users/password(.:format)  {:action=>"update", :controller=>"devise/passwords"} 
    cancel_user_registration GET /users/cancel(.:format)  {:action=>"cancel", :controller=>"devise/registrations"} 
    user_registration POST /users(.:format)    {:action=>"create", :controller=>"devise/registrations"} 
    new_user_registration GET /users/sign_up(.:format)  {:action=>"new", :controller=>"devise/registrations"} 
    edit_user_registration GET /users/edit(.:format)   {:action=>"edit", :controller=>"devise/registrations"} 
        PUT /users(.:format)    {:action=>"update", :controller=>"devise/registrations"} 
        DELETE /users(.:format)    {:action=>"destroy", :controller=>"devise/registrations"} 
      testers GET /testers(.:format)    {:action=>"index", :controller=>"testers"} 
        POST /testers(.:format)    {:action=>"create", :controller=>"testers"} 
      new_tester GET /testers/new(.:format)   {:action=>"new", :controller=>"testers"} 
     edit_tester GET /testers/:id/edit(.:format) {:action=>"edit", :controller=>"testers"} 
       tester GET /testers/:id(.:format)   {:action=>"show", :controller=>"testers"} 
        PUT /testers/:id(.:format)   {:action=>"update", :controller=>"testers"} 
        DELETE /testers/:id(.:format)   {:action=>"destroy", :controller=>"testers"} 
       root  /       {:controller=>"testers", :action=>"index"} 

在应用控制器我试图覆盖的方法等,但以下以无济于事我仍然被路由回测试者索引

class ApplicationController < ActionController::Base 

    protect_from_forgery 

    def after_sign_in_path_for(resource) 

     new_tester_path 

    end 

end 

在我的routes.rb文件,我有以下线

Testing::Application.routes.draw do 

    devise_for :users 

    resources :testers 

    root :to => 'testers#index' 

尽管大部分代码是用脚手架做我仍然无法弄清楚如何重定向到new_tester_path或路线/测试人员/新成功sign_in由用户电子邮件和密码。

有人可以让我知道我失踪.....在写覆盖函数时,我想知道确切的路线,我需要指定。

测试时我试图这样,但谷歌网页还没有打开一些愚蠢... :(

class ApplicationController < ActionController::Base 

protect_from_forgery 

helper ApplicationHelper 

def after_sign_in_path_for(resource) 

"www.google.com" 

end 

def after_sign_up_path_for(resource) 

"www.google.com" 

end 

def after_update_path_for(resource) 

"www.google.com" 

end 
+0

测试时我试图这样,但一些愚蠢的谷歌网页还没有打开... :(类的ApplicationController

回答

5

尝试在会话设置user_return_to路径:

session['user_return_to'] = new_tester_path 

你可以这样做在从Devise :: SessionsController派生出来的控制器中

+0

我想这和路由工作对我来说DEF stored_location_for(资源) 零 结束 –

0

您可以尝试在Rails应用程序中隔离这个问题并在Devise上打开一个问题吗?

+0

我试图ater_sign_in_path高清stored_location_for(资源) 零 年底前覆盖以下功能----它只是工作 - 发现这个函数在与stackoverflow上的设计问题相关的线程之一 –

1

我认为这是一个继承问题。 after_sign_in_path_for最初是在Devise :: SessionsController中定义的。你可以通过使你的SessionsController继承自Devise :: SessionsController,然后在控制器中重新定义它来覆盖它。

19

只要使用这个片段:

class ApplicationController < ActionController::Base 
    def after_sign_in_path_for(user) 
     user_url(user) 
    end 
end 
1

如果您在试图重写Rails的发动机的ApplicationController内after_sign_in_path_forafter_sign_out_path_for辅助方法的问题,你可能想看看这个answer

它描述了如何重写引擎中的SessionsController而不是ApplicationController。

0

设计文档解释了所有步骤redirect to a specific page on successful sign in。通过结合这些技术,您可以在成功登录后将用户重定向到许多地方。

这里是简历:您从设计:: SessionsController继承

您可以在控制器做到这一点 - 首先,在控制器/用户/ sessions_controller.rb:

module Users 
    class SessionsController < Devise::SessionsController 
    def new 
    if params[:redirect_to].present? 
     self.resource = resource_class.new(sign_in_params) 
     store_location_for(resource, params[:redirect_to]) 
    end 
    super 
    end 
    end 
end 

在配置/路线.RB,你也补充道:

devise_for :users, controllers: {sessions: 'users/sessions'} 

而且你必须在你的ApplicationController添加自定义after_sign_in_path_for

class ApplicationController < ActionController::Base 
    protected 
    def after_sign_in_path_for(resource) 
    stored_location_for(resource) || root_path 
    end 
end 

这可以在所有Devise版本中使用,据我所知。

+0

你确定它是'new'而不是'create'?当'GET'到'#new'路线时,你会如何获得'sign_in_params'? – Chloe