2011-02-14 159 views
17

設計給我一個相當困難的時間。目前,除了註冊重定向之外,其他一切似乎都在起作用。我想設計重定向到我的鎮控制器在索引行動,註冊或登錄(登錄實際工作)。註冊後重新設計

我試着重寫RegistrationsController,我已經嘗試添加一個applicationsController功能,如:

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

認真,我無法找到一種方法來做到這一點。請有任何想法嗎? :)

編輯: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:

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

顯示我們通過`$耙routes`請 – raidfive 2011-02-14 00:17:17

+0

感謝名單的路線,我添加了相關的路線 – Spyros 2011-02-14 00:23:27

+1

向我們展示你的routes.rb內的色器件指令。從外觀來看,資源不是用戶。找出它實際上是哪個類。 – Dex 2011-02-14 01:33:40

回答

11

這就是我如何得到這個工作。

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

然後確保你沒有你的home#index控制器上的before_filter :authenticate_user!集。

6

因爲沒有人接,我會附和你確定這是這是造成錯誤的代碼?你的resource_or_scope爲零使得它看起來像設計是問題,但我懷疑是這樣。所以我認爲問題在別的地方。

我想嘗試一下以確保它首先工作。

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

如果這樣做工作,然後嘗試檢查resource變量,以確保它不是零。

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 
0

我有類似的問題。最終訴諸於應用程序控制器中的if語句。如果用戶登錄,他們沒有配置文件,然後路由到new_profile_path

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和1.3.0制定對我的用戶類型正確的道路。

14

在Ruby On Rails 3.0.7中使用Devise 1.3.4。檢查出一個解決方案互聯網後,我所做的就是簡單地粘貼下面的代碼

* 第一) *爲一個成功的標誌了歡迎頁面重定向後,發生在/config/routes.rb以下(注意,你提供給devise_for參數替換:user):

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

*秒) *要在退出後重定向(歡迎頁面也),地點在/ app /控制器/ 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

的問題是,設計登錄到剛剛創建的資源時繞過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)覆蓋到後續登錄插件正確地重定向到資源。

1

託尼說了什麼。命名空間並不意味着以這種方式使用。我不知道是誰發明了這種根本不起作用的黑客,但不幸的是,這是Google搜索時出現的第一個解決方案之一。

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

套「/用戶」爲user_root_path並指出「用戶/鎮」控制器的index動作,你沒有。你有什麼是'鎮'控制器。這就是你得到錯誤的原因。你需要的是

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

這樣,「/鎮」指向「鎮#指數」,並設置爲你的user_root_path,這是設計重定向到用戶後

  1. 登錄
  2. 註冊
  3. 更新密碼

,所以你甚至都不需要重寫after_sign_in_path_for,其中,反之爲w許多人認爲,這不是解決設計重定向問題的最佳地點。

on Rails的4.1+和設計3.2