Reputation: 71
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()?
Upvotes: 2
Views: 826
Reputation: 9529
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.
Upvotes: 1