Ruby-on-rails 注册后设计重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4987841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:25:17  来源:igfitidea点击:

Devise redirect after sign up

ruby-on-rails

提问by Spyros

Devise is giving me a pretty hard time. For now, everything else seems to be working, except the sign up redirect. I want devise to redirect to my town controller at index action, upon sign up or login (login actually works).

设计让我很难过。目前,除了注册重定向之外,其他一切似乎都在工作。我想设计在索引操作时重定向到我的城镇控制器,在注册或登录时(登录实际上有效)。

I've tried overriding RegistrationsController and i've tried adding an applicationsController function like :

我试过覆盖 RegistrationsController 并尝试添加一个 ApplicationsController 函数,如:

  def after_sign_in_path_for(resource_or_scope)
    if resource_or_scope.is_a?(User)
      town_path
    else
      super
    end
  end

Still, i'm getting the same error :

尽管如此,我还是遇到了同样的错误:

NoMethodError in User/townController#index

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.*

Seriously, i cannot find a way to do it. Any ideas please ? :)

说真的,我找不到办法做到这一点。请问有什么想法吗?:)

EDIT : MY ROUTES

编辑:我的路线

      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 GET    /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"}
     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"}
                  root        /(.:format)                                    {:action=>"index", :controller=>"home"}
             user_root        /user(.:format)                                {:action=>"index", :controller=>"user/town"}
                  home        /home(.:format)                                {:action=>"index", :controller=>"home"}
                  town        /town(.:format)                                {:action=>"index", :controller=>"town"}
                 inbox        /messages(.:format)                            {:action=>"index", :controller=>"messages"}
                 inbox        /messages/inbox(.:format)                      {:action=>"inbox", :controller=>"messages"}

Routes.rb :

路线.rb :

  devise_for :users

  root :to => "home#index"

  namespace :user do
    root :to => "town#index"
  end  

  scope :path => '/home', :controller => :home do
    match '/' => :index, :as => 'home'
  end

  scope :path => '/town', :controller => :town do
    match '/' => :index, :as => 'town'
  end
......

采纳答案by Tony

This is how I got this working.

这就是我的工作方式。

# In your routes.rb
match 'dashboard' => 'user_dashboard#index', :as => 'user_root'

Then make sure you don't have a before_filter :authenticate_user!set on your home#indexcontroller.

然后确保您before_filter :authenticate_user!home#index控制器上没有设置。

回答by Kike

Im using Devise 1.3.4 with Ruby On Rails 3.0.7. After checking out the internet for a solution, what i did was simply pasting the following code

我使用 Devise 1.3.4 和 Ruby On Rails 3.0.7。在上网寻找解决方案后,我所做的只是粘贴以下代码

*first)*To redirect after a succesful sign up to the welcome page,place in /config/routes.rb the following (note, replace :userwith the argument you provided to devise_for):

*首先)*要在成功注册后重定向到欢迎页面,请在 /config/routes.rb 中放置以下内容(注意,替换:user为您提供给的参数devise_for):

namespace :user do
root :to => "welcome#index"
end

*second)*To redirect after a sign out (to the welcome page also), place in the /app/controllers/application_controller.rb this method:

*第二)*要在注销后重定向(也到欢迎页面),请将此方法放在 /app/controllers/application_controller.rb 中:

private
# Overwriting the sign_out redirect path method
def after_sign_out_path_for(resource_or_scope)
welcome_index_path
end

This worked for me, i hope it does for all of you.

这对我有用,我希望它对你们所有人有用。

回答by cailinanne

An answer to this question that worked perfectly for me is explained on the Devise Wiki here:
How To: Redirect to a specific page on successful sign up (registration)

Devise Wiki 上解释了对我来说非常有用的这个问题的答案:
How To: Redirect to a specific page on success sign up (registration)

回答by Dty

Since no one is answering I'll chime in. Are you sure this is the code that's causing the error? Your resource_or_scopebeing nil makes it seem like Devise is the problem but I doubt that's the case. So I think the problem is somewhere else.

由于没有人回答我会插话。您确定这是导致错误的代码吗?你resource_or_scope的零使得它看起来像设计是问题,但我怀疑是这种情况。所以我认为问题出在其他地方。

I'd try the following just to make sure it works first.

我会尝试以下只是为了确保它首先起作用。

  def after_sign_in_path_for(resource)
    "http://www.google.com"
  end

If that does work then try checking the resourcevariable to make sure it's not nil.

如果确实有效,请尝试检查resource变量以确保它不为零。

回答by kriysna

Did you tried overwriting devise sign_in_and_redirectmethod; It will work for you

您是否尝试过覆盖设计sign_in_and_redirect方法;它会为你工作

def sign_in_and_redirect(resource_or_scope,resource)
    if resource_or_scope == :user
      redirect_to town_path
    else
      super
    end
end

回答by Sam G

What Tony said. Namespace is not meant to be used this way. I don't know who came up with this hack that doesn't work at all, but unfortunately, it's one of the very first solutions that come up when googling.

托尼说的。命名空间不应该以这种方式使用。我不知道是谁想出了这个根本不起作用的黑客,但不幸的是,这是谷歌搜索时出现的第一个解决方案之一。

namespace :user do
  root :to => "town#index"
end

sets '/user' as the user_root_path and points to the index action of 'user/town' controller which you don't have. What you have is 'town' controller. And that's why you got the error. What you need is

将 '/user' 设置为 user_root_path 并指向您没有的 'user/town' 控制器的索引操作。您拥有的是“城镇”控制器。这就是你收到错误的原因。你需要的是

get '/town', to: 'town#index', as: 'user_root'

This way, '/town' points to 'town#index' and is set up as your user_root_path, which is what Devise redirects to after user

这样,'/town' 指向 'town#index' 并设置为您的 user_root_path,这是 Devise 在用户之后重定向到的

  1. sign in
  2. sign up
  3. update password
  1. 登入
  2. 报名
  3. 更新密码

so you do not even need to override after_sign_in_path_for, which, contrary to what many believe, is not the best place to tackle the devise redirect problem.

所以你甚至不需要覆盖after_sign_in_path_for,与许多人认为的相反,这不是解决设计重定向问题的最佳位置。

on Rails 4.1+ and Devise 3.2

在 Rails 4.1+ 和 Devise 3.2 上

回答by 29 Steps

Not sure if this is helpful or not but I ran into the same problems above but difference is I'm using a custom registrations controller.

不确定这是否有帮助,但我遇到了上述相同的问题,但不同之处在于我使用的是自定义注册控制器。

From the devise source code for RegistrationsController it appears it is calling sign_in_and_redirect which in turns passes it to redirect_location method.

从 RegistrationsController 的设计源代码来看,它似乎正在调用 sign_in_and_redirect,后者又将其传递给 redirect_location 方法。

I override the redirect_location to just return the path from after_sign_up_path_for(resource) rather than stored_location_for(scope) || after_sign_up_path_for(resource).

我覆盖了redirect_location 以只返回来自after_sign_up_path_for(resource) 而不是stored_location_for(scope) || 的路径 after_sign_up_path_for(资源)。

Within after_sign_up_path_for(resource), I customise the method to redirect to the right path based on the type of user

在after_sign_up_path_for(resource)中,我根据用户类型自定义重定向到正确路径的方法

Works in Rails 3.0.5 and Devise 1.3.0 for me.

为我在 Rails 3.0.5 和 Devise 1.3.0 中工作。

回答by zeantsoi

The issue is that Devise bypasses after_sign_in_path_for(resource)when signing into a resource that has just been created. What you need is after_sign_up_path_for(resource):

问题是 Deviseafter_sign_in_path_for(resource)在登录到刚刚创建的资源时会绕过。你需要的是after_sign_up_path_for(resource)

# override in your ApplicationController or custom Devise::RegistrationsController  
def after_sign_up_path_for(resource)
  if resource.is_a?(User)
    town_path
  else
    super
  end
end

You'll still need your after_sign_in_path_for(resource)override to correctly redirect subsequent sign ins to the resource.

您仍然需要您的after_sign_in_path_for(resource)替代来正确地将后续登录重定向到资源。

回答by Gary

I had a similar problem. Eventually resorted to if statement in application controller. If user signed in and they do not have a profile then route to the new_profile_path

我有一个类似的问题。最终求助于应用程序控制器中的 if 语句。如果用户登录并且他们没有个人资料,则路由到 new_profile_path