开发者

Rails 3 domain-relative url

In Rails 2, you could add a leading slash to the :controller param to get a domain-relative url. Such as this:

# anywhere in the app, Rails 2
>> url_for(:controller => '/posts', :action => 'index')
=> "/posts"

However, in Rails 3, this appears to not work if you are in a deeply nested controller. For instance, if I'm in Home::Foo, it works. If I'm in Home::Foo::Bar, it doesn't work:

# in Home, works
>> url_for(:controller => '/posts', :action => 'index')
=> "/posts"

# in Home::Foo, works
>> url_for(:controller => '/posts', :action => 'index')
=> "/posts"

# in Home::Foo::Bar, does not work
>> url_for(:controller => '/posts', :action开发者_开发知识库 => 'index')
=> "home//posts"

Question is: what is the proper Rails 3 way of getting a domain-relative link using url_for()?


Can't you just use the restful path of posts?

routes.rb

map.resources :posts

anyview.html.erb

posts_path

That should generate '/posts' wherever you are at.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜