Tim Santeford
Tim Santeford

Reputation: 28151

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', :action => '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/

Upvotes: 24

Views: 23477

Answers (3)

eveevans
eveevans

Reputation: 4460

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)

Upvotes: 1

Milan Novota
Milan Novota

Reputation: 15598

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. ;)

Upvotes: 28

Ryan Bigg
Ryan Bigg

Reputation: 107738

Use root_url. The _url extension on the end gives the full URL, something like http://localhost:3000/projects/1/tickets/2.

Upvotes: 46

Related Questions