注册后设计重定向

时间:2011-02-14 00:15:48

标签: ruby-on-rails

Devise给了我一个非常艰难的时刻。现在,除了注册重定向之外,其他一切似乎都在起作用。我希望设计在注册或登录时重定向到我的城镇控制器(登录实际上有效)。

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

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

仍然,我得到了同样的错误:

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.*

说真的,我找不到办法去做。有什么想法吗? :)

编辑:我的路线

      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:

  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
......

9 个答案:

答案 0 :(得分:14)

我使用Devise 1.3.4和Ruby On Rails 3.0.7。在检查了互联网的解决方案后,我所做的只是粘贴以下代码

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

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

* second) *要在注销后重定向(也到欢迎页面),请将此方法放在/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

这对我有用,我希望它适用于所有人。

答案 1 :(得分:11)

这就是我的工作方式。

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

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

答案 2 :(得分:9)

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

答案 3 :(得分:6)

由于没有人回答,我会插话。你确定这是造成错误的代码吗?你的resource_or_scope nil使得看起来像Devise是问题,但我怀疑是这样的。所以我认为问题出在其他地方。

我会尝试以下方法,以确保它首先发挥作用。

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

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

答案 4 :(得分:2)

您是否尝试覆盖设计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

答案 5 :(得分:2)

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

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

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

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

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

  1. 登录
  2. 注册
  3. 更新密码
  4. 所以你甚至不需要覆盖after_sign_in_path_for,这与许多人认为不是解决设计重定向问题的最佳位置相反。

    on Rails 4.1+和Devise 3.2

答案 6 :(得分:1)

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

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

我重写redirect_location只返回after_sign_up_path_for(resource)的路径而不是stored_location_for(scope)|| after_sign_up_path_for(资源)。

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

适用于Rails 3.0.5和Devise 1.3.0。

答案 7 :(得分:1)

问题在于,当签署刚刚创建的资源时,Devise会绕过after_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

您仍然需要after_sign_in_path_for(resource)覆盖才能将后续登录正确地重定向到资源。

答案 8 :(得分:0)

我有类似的问题。最终在应用程序控制器中使用if语句。如果用户已登录且他们没有配置文件,则转到new_profile_path

相关问题