2011-09-13 67 views
0

我的項目中有許多用戶可用。每個用戶有不同的主頁。而且我還有一個默認主頁。我的實際代碼是在這裏..導軌 - 重定向問題

requested_url = "/limited/username" #It is constantly changing. 
redirect_to(requested_url || :action => "index", :controller => "demo") 

因此,它重定向頁面像這樣http://localhost:3000/demo/index?%2Flimited=username。但是,實際上我需要重定向的url,像這樣http://localhost:3000/limited/username

如果requested_url爲空,那麼它會正確重定向。 (http://localhost:3000/demo/index)。但是,如果它不是空的,它會錯誤地重定向。

請告訴我這裏有什麼問題?

回答

6

||=>更高的運算符優先級,讓您的重定向調用將被視爲

redirect_to((requested_url || :action) => "index", :controller => "demo") 

試試這個:

redirect_to(requested_url || {:action => "index", :controller => "demo"}) 
+0

演示控制器不是常用的控制器。 「demo/index」和「/限制/用戶名」都是分開的。抱歉!。我不喜歡使用if語句。我稍微修改了我的代碼,如redirect_to(requested_url ||「demo/index」)。現在,我的問題解決了。感謝您記住運營商的優先權。 –

0

很顯然,我並不瞭解的具體細節你在這裏嘗試做,但你也許可以做這樣的事情:

before_filter :redirect_to_requested_url, :if => :requested_url_supplied? 

def redirect_to_requested_url 
    redirect_to ... get requested url somehow ... 
end