【问题标题】:Devise after_sign_in_path_for ... sending to .... root_path - query设计 after_sign_in_path_for ... 发送到 .... root_path - 查询
【发布时间】:2012-01-04 06:18:45
【问题描述】:

我需要有关设计身份验证 gem 的路由问题的帮助,以便在成功登录后重定向到自定义页面,以便通过输入测试人员姓名和年龄(测试数据)来创建新记录

我正在使用带有设计版本 1.4.9 的 Rails 3

我的路线如下

    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 或路由 /testers/new通过用户电子邮件和密码登录

有人可以告诉我我缺少什么.....在编写覆盖函数时,我想知道我需要指定的确切路线。

在测试时,我尝试了类似这样的愚蠢操作,但谷歌页面也没有打开...... :(

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 

【问题讨论】:

  • 在测试时我尝试了类似这样的愚蠢操作,但谷歌页面也没有打开...... :( class ApplicationController google.com " end def after_sign_up_path_for(resource_or_scope) "google.com" end def after_update_path_for(resource_or_scope) "google.com" end

标签: ruby-on-rails ruby-on-rails-3 devise routes


【解决方案1】:

只需使用这个 sn-p:

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

【讨论】:

    【解决方案2】:

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

    session['user_return_to'] = new_tester_path
    

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

    【讨论】:

    • 我试过了,这些路线对我有用 def stored_location_for(resource) nil end
    【解决方案3】:

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

    这是简历:

    您可以在从 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
    

    在 config/routes.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 版本。

    【讨论】:

    • 您确定是new 而不是create?当您 GET #new 路由时,您将如何使用 sign_in_params
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      如果您在尝试覆盖 Rails 引擎的 ApplicationController 中的 after_sign_in_path_forafter_sign_out_path_for 辅助方法时遇到问题,您可能需要查看此 answer

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

      【讨论】:

        【解决方案6】:

        你似乎没有做错什么。也许这是一个设计问题。

        您能否尝试在 Rails 应用程序上隔离此问题并在 Devise 上打开问题?

        【讨论】:

        • 我试图在 ater_sign_in_path def stored_location_for(resource) nil end ---- 之前覆盖下面的函数,它刚刚起作用——在与 stackoverflow 上的设计问题相关的线程之一中找到了这个函数跨度>
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-27
        • 1970-01-01
        • 1970-01-01
        • 2015-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多