设计after_sign_in_path_for ...发送到... root_path - 查询

时间:2022-10-08 23:54:03

I need help with a routes issue with devise authentication gem to redirect to a custom page after successful login so as to create a new record by entering a test person name and age ( test data )

我需要帮助解决路由问题,使用devise authentication gem在成功登录后重定向到自定义页面,以便通过输入测试人员姓名和年龄(测试数据)来创建新记录

I am using Rails 3 with devise version 1.4.9

我正在使用Rails 3和设计版本1.4.9

My routes are as below

我的路线如下

    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"}

In applications controller i tried to override the method like below but to no avail i still get routed back to tester index

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

class ApplicationController < ActionController::Base

  protect_from_forgery

  def after_sign_in_path_for(resource) 

      new_tester_path

  end

end

In my routes.rb file i have the below lines

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

Testing::Application.routes.draw do

  devise_for :users 

  resources :testers

  root :to => 'testers#index'

While much of the code was done with scaffolding I was still not be able to figure how to redirect to new_tester_path or route /testers/new after successful sign_in by user email and password.

虽然大部分代码都是使用脚手架完成的,但在用户电子邮件和密码成功登录后,我仍然无法确定如何重定向到new_tester_path或route / testers / new。

Can someone please let me know what i am missing..... while writing the override function, I would like to know the exact route i need to specify.

有人可以让我知道我错过了什么.....在编写覆盖功能时,我想知道我需要指定的确切路线。

While testing i tried something stupid like this but the google page is also not opening ... :(

虽然测试我尝试了这样的愚蠢,但谷歌页面也没有打开...... :(

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 

6 个解决方案

#1


20  

Just use this snippet:

只需使用此代码段:

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

#2


5  

Try setting user_return_to path in session:

尝试在会话中设置user_return_to路径:

session['user_return_to'] = new_tester_path

You can do it in a controller derived from Devise::SessionsController

您可以在从Devise :: SessionsController派生的控制器中执行此操作

#3


1  

I believe this is an inheritance issue. after_sign_in_path_for is originally defined within Devise::SessionsController. You can override it by making your SessionsController inherit from Devise::SessionsController, and then re-defining it within that controller.

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

#4


1  

If you are having issues trying to override the after_sign_in_path_for or after_sign_out_path_for helper methods within the ApplicationController of a Rails engine, you may want to check out this answer.

如果您在尝试覆盖Rails引擎的ApplicationController中的after_sign_in_path_for或after_sign_out_path_for辅助方法时遇到问题,您可能需要查看此答案。

It describes how you'll need to override the SessionsController in your engine instead of the ApplicationController.

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

#5


0  

You don't seem to be doing anything wrong. Maybe this is a Devise issue.

你似乎没有做错任何事。也许这是一个设计问题。

Can you please try to isolate this on a Rails app and open an issue on Devise ?

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

#6


0  

The Devise documentation explains all the steps to redirect to a specific page on successful sign in. By combine the techniques, you can redirect a user to many places after successful sign in.

Devise文档说明了在成功登录后重定向到特定页面的所有步骤。通过组合这些技术,您可以在成功登录后将用户重定向到多个位置。

Here is the resume:

这是简历:

You can do this in a controller you inherit from Devise::SessionsController - first, in controllers/users/sessions_controller.rb:

您可以在从Devise :: SessionsController继承的控制器中执行此操作 - 首先,在controllers / users / 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

In config/routes.rb, you would have also added:

在config / routes.rb中,您还会添加:

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

And you must add a custom after_sign_in_path_for in your ApplicationController

您必须在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

This works in all Devise versions, as I know.

据我所知,这适用于所有Devise版本。

#1


20  

Just use this snippet:

只需使用此代码段:

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

#2


5  

Try setting user_return_to path in session:

尝试在会话中设置user_return_to路径:

session['user_return_to'] = new_tester_path

You can do it in a controller derived from Devise::SessionsController

您可以在从Devise :: SessionsController派生的控制器中执行此操作

#3


1  

I believe this is an inheritance issue. after_sign_in_path_for is originally defined within Devise::SessionsController. You can override it by making your SessionsController inherit from Devise::SessionsController, and then re-defining it within that controller.

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

#4


1  

If you are having issues trying to override the after_sign_in_path_for or after_sign_out_path_for helper methods within the ApplicationController of a Rails engine, you may want to check out this answer.

如果您在尝试覆盖Rails引擎的ApplicationController中的after_sign_in_path_for或after_sign_out_path_for辅助方法时遇到问题,您可能需要查看此答案。

It describes how you'll need to override the SessionsController in your engine instead of the ApplicationController.

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

#5


0  

You don't seem to be doing anything wrong. Maybe this is a Devise issue.

你似乎没有做错任何事。也许这是一个设计问题。

Can you please try to isolate this on a Rails app and open an issue on Devise ?

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

#6


0  

The Devise documentation explains all the steps to redirect to a specific page on successful sign in. By combine the techniques, you can redirect a user to many places after successful sign in.

Devise文档说明了在成功登录后重定向到特定页面的所有步骤。通过组合这些技术,您可以在成功登录后将用户重定向到多个位置。

Here is the resume:

这是简历:

You can do this in a controller you inherit from Devise::SessionsController - first, in controllers/users/sessions_controller.rb:

您可以在从Devise :: SessionsController继承的控制器中执行此操作 - 首先,在controllers / users / 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

In config/routes.rb, you would have also added:

在config / routes.rb中,您还会添加:

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

And you must add a custom after_sign_in_path_for in your ApplicationController

您必须在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

This works in all Devise versions, as I know.

据我所知,这适用于所有Devise版本。