Full URL with url_for in Rails

How can I get the full url in rails?

url_for @book only returns a path, e.g. / book / 1, not www.domain.com/book/1

Thanks (and sorry if the answer is obvious. I'm learning the rails!)

+21
ruby-on-rails
Aug 16 2018-10-10T00:
source share
6 answers

Use the parameter: host. For example, you can use:

url_for(@book, :host => "domain.com") 
+1
Aug 16 '10 at 0:26
source share

According to the documents, this should not happen. The option you are looking for is :only_path , and by default it is false. What happens if you explicitly set the value to false?

 url_for(@book, :only_path => false) 

While you can use url_for , you should prefer the Ryan method when you can - book_url(@book) for the full URL or book_path(@book) for the path.

+63
Aug 16 '10 at 1:12
source share

If it is a RESTful resource, you can use this:

 book_url(@book) 
+22
Aug 16 '10 at 1:05
source share

In Rails 4, url_for accepts only one argument , so you need to pass an array with an explicit hash inside for the only_path parameter.

Good:

 url_for([@post, @comment, {only_path: true}]) 

Poorly:

 url_for(@post, @comment, {only_path: true}) url_for([@post, @comment], {only_path: true}) 

From url_for source with Array input only calls:

 polymorphic_url([@post, @comment], {only_path: true}) 

as shown in @moose's answer.

As @lime noted, only_path usually not required for polymorphic_url , since you distinguish it by _url _path suffixes.

+13
Sep 02 '14 at 23:10
source share

This seems to work:

 url_for(@book) 

But this is not so. The url_for method accepts only one argument, which can be either a string, or an instance of a model, or an hash of options. This is pretty unfortunate, since it looks like you might need a link to @book and add options like: only_path or: host.

One way is to use polymorphic_url, which will display the correct absolute URL, even if your model (most likely) is not actually polymorphic:

 polymorphic_url(@book, :host => "domain.com") 

Perhaps the best route would be to use a named route, which is automatically set for you when you declare resources on your routes or using the: as option:

 # in routes.rb: resources :books # or get "books/:id" => "books#show", as: :book # in your view: book_path(@book, :host => "domain.com") 
+4
Jan 20 '13 at 4:30
source share

In Rails 5, if you want url_for for the current controller / action (== of the current page), just use url_for(only_path: false)

Long answer:

In Rails 5, the url_for in the view is ActionView::RoutingUrlFor#url_for . If you look at its source code ( https://api.rubyonrails.org/classes/ActionView/RoutingUrlFor.html#method-i-url_for ), you will see if you passed the hash (the keyword parameters are converted to Ruby hash), it actually calls super , invoking a method with the same name from its ancestor.

ActionView::RoutingUrlFor.ancestors shows that its first ancestor is ActionDispatch::Routing::UrlFor .

By checking its source code ( https://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html#method-i-url_for ), you will read this:

The keys of the missed routes can be filled out from the parameters of the current request (for example, controller ,: action ,: id and any other parameters that are placed in the path).

This is very good, as it will automatically create a URL for you for the current page (or path, if you just call url_for without only_path: false ). It will also intelligently ignore query string parameters; if you need to combine them, you can use url_for(params.merge({arbitrary_argument:'value'})) .

0
Apr 03 '19 at 16:15
source share



All Articles