Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full URL with url_for in Rails

How can I get a full url in rails?

url_for @book is returning only a path like /book/1 and not www.domain.com/book/1

Thanks (and sorry if the answer is obvious. Im learning rails!)

like image 906
Victor Avatar asked Sep 10 '25 01:09

Victor


2 Answers

According to the docs, this shouldn't happen. The option you're looking for is :only_path and it's false by default. What happens if you set it to false explicitly?

url_for(@book, :only_path => false)

While you can use url_for you should prefer Ryan's method when you can - book_url(@book) for a full url or book_path(@book) for the path.

like image 199
Andy Gaskell Avatar answered Sep 12 '25 15:09

Andy Gaskell


If it's a RESTful resource you'll be able to use this:

book_url(@book)
like image 30
Ryan Bigg Avatar answered Sep 12 '25 16:09

Ryan Bigg