注册后设计重定向

Spy*_*ros 18 ruby-on-rails

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

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

  def after_sign_in_path_for(resource_or_scope)
    if resource_or_scope.is_a?(User)
      town_path
    else
      super
    end
  end
Run Code Online (Sandbox Code Playgroud)

不过,我得到了同样的错误:

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.*
Run Code Online (Sandbox Code Playgroud)

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

编辑:我的路线

      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"}
Run Code Online (Sandbox Code Playgroud)

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
......
Run Code Online (Sandbox Code Playgroud)

小智 14

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

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

namespace :user do
root :to => "welcome#index"
end
Run Code Online (Sandbox Code Playgroud)

*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
Run Code Online (Sandbox Code Playgroud)

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


小智 11

这就是我的工作方式.

# In your routes.rb
match 'dashboard' => 'user_dashboard#index', :as => 'user_root'
Run Code Online (Sandbox Code Playgroud)

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


cai*_*nne 9

这个问题的答案对我来说非常有用,在Devise Wiki上有解释:
如何:成功注册时重定向到特定页面(注册)


Dty*_*Dty 6

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

我会尝试以下方法,以确保它首先工作.

  def after_sign_in_path_for(resource)
    "http://www.google.com"
  end
Run Code Online (Sandbox Code Playgroud)

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