2011-09-13 68 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