How do I get the absolute path of a resource in Rails 3?
I would like to get the full absolute path to a resource including the domain name in Rails 3.
Example: to get the full path to the home page I have tried:
url_for( :controller => 'home', :开发者_如何学Pythonaction => 'index' )
and
root_path
but both give me just: /
but I want: http://www.example.com/
The answer should also work on my dev server and return: http://localhost:3000/
Use root_url
. The _url
extension on the end gives the full URL, something like http://localhost:3000/projects/1/tickets/2
.
Ryan's answer is perfectly correct.
However, if you really needed to use url_for
someday, just add :only_path => false
to it's options and it'll make the url absolute for you. ;)
If you want the resouce url (instead of only the root url). You have to use resoure_name_url(@resource_instance)
Example:
resource_url(@resource_instance)
Or if you have (for example) the users resource:
user_url(@user)
精彩评论