How can I get a full url in rails?
如何在rails中获得完整的url?
url_for @book is returning only a path like /book/1 and not www.domain.com/book/1
url_for @book只返回像/ book / 1这样的路径而不是www.domain.com/book/1
Thanks (and sorry if the answer is obvious. Im learning rails!)
谢谢(如果答案很明显,那就很抱歉。我正在学习铁路!)
5 个解决方案
#1
0
Use the :host option. For example, you can use:
使用:host选项。例如,您可以使用:
url_for(@book, :host => "domain.com")
#2
62
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?
根据文档,这不应该发生。您正在寻找的选项是:only_path,默认情况下为false。如果将其明确设置为false会发生什么?
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.
虽然你可以使用url_for,但你可以选择Ryan的方法 - book_url(@book)获取路径的完整url或book_path(@book)。
#3
22
If it's a RESTful resource you'll be able to use this:
如果它是RESTful资源,您将能够使用它:
book_url(@book)
#4
13
In Rails 4, url_for only takes one argument, so you need to pass an array with an explicit hash inside for the only_path
option.
在Rails 4中,url_for只接受一个参数,因此您需要为only_path选项传递一个内部显式哈希的数组。
Good:
好:
url_for([@post, @comment, {only_path: true}])
Bad:
坏:
url_for(@post, @comment, {only_path: true})
url_for([@post, @comment], {only_path: true})
From the source, url_for
with an Array
input just calls:
从源代码中,带有Array输入的url_for只调用:
polymorphic_url([@post, @comment], {only_path: true})
as shown in @moose's answer.
如@ moose的回答所示。
As noted by @lime, only_path
is generally not needed for polymorphic_url
since you distinguish that with the _url
_path
suffixes.
正如@lime所指出的那样,对于polymorphic_url通常不需要only_path,因为您将其与_url _path后缀区分开来。
#5
4
It seems that this would work:
看起来这会起作用:
url_for(@book)
But it does not. The url_for method accepts only one argument, which can be either a string, an instance of a model, or a hash of options. This is rather unfortunate, as it would seem like you may need to link to @book and add options like :only_path or :host as well.
但事实并非如此。 url_for方法只接受一个参数,该参数可以是字符串,模型的实例或选项的哈希。这是相当不幸的,因为您可能需要链接到@book并添加以下选项:only_path或:host。
One way around it is to use polymorphic_url, which would render the correct absolute url even though your model is (likely) not actually polymorphic:
解决它的一种方法是使用polymorphic_url,即使您的模型(可能)实际上不是多态的,它也会呈现正确的绝对URL:
polymorphic_url(@book, :host => "domain.com")
Perhaps the best route would be to use a named route, which is set up automatically for you when declaring resources in your routes or using the :as option:
也许最好的路线是使用命名路由,在路由中声明资源或使用:as选项时自动为您设置:
# in routes.rb:
resources :books
# or
get "books/:id" => "books#show", as: :book
# in your view:
book_path(@book, :host => "domain.com")
#1
0
Use the :host option. For example, you can use:
使用:host选项。例如,您可以使用:
url_for(@book, :host => "domain.com")
#2
62
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?
根据文档,这不应该发生。您正在寻找的选项是:only_path,默认情况下为false。如果将其明确设置为false会发生什么?
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.
虽然你可以使用url_for,但你可以选择Ryan的方法 - book_url(@book)获取路径的完整url或book_path(@book)。
#3
22
If it's a RESTful resource you'll be able to use this:
如果它是RESTful资源,您将能够使用它:
book_url(@book)
#4
13
In Rails 4, url_for only takes one argument, so you need to pass an array with an explicit hash inside for the only_path
option.
在Rails 4中,url_for只接受一个参数,因此您需要为only_path选项传递一个内部显式哈希的数组。
Good:
好:
url_for([@post, @comment, {only_path: true}])
Bad:
坏:
url_for(@post, @comment, {only_path: true})
url_for([@post, @comment], {only_path: true})
From the source, url_for
with an Array
input just calls:
从源代码中,带有Array输入的url_for只调用:
polymorphic_url([@post, @comment], {only_path: true})
as shown in @moose's answer.
如@ moose的回答所示。
As noted by @lime, only_path
is generally not needed for polymorphic_url
since you distinguish that with the _url
_path
suffixes.
正如@lime所指出的那样,对于polymorphic_url通常不需要only_path,因为您将其与_url _path后缀区分开来。
#5
4
It seems that this would work:
看起来这会起作用:
url_for(@book)
But it does not. The url_for method accepts only one argument, which can be either a string, an instance of a model, or a hash of options. This is rather unfortunate, as it would seem like you may need to link to @book and add options like :only_path or :host as well.
但事实并非如此。 url_for方法只接受一个参数,该参数可以是字符串,模型的实例或选项的哈希。这是相当不幸的,因为您可能需要链接到@book并添加以下选项:only_path或:host。
One way around it is to use polymorphic_url, which would render the correct absolute url even though your model is (likely) not actually polymorphic:
解决它的一种方法是使用polymorphic_url,即使您的模型(可能)实际上不是多态的,它也会呈现正确的绝对URL:
polymorphic_url(@book, :host => "domain.com")
Perhaps the best route would be to use a named route, which is set up automatically for you when declaring resources in your routes or using the :as option:
也许最好的路线是使用命名路由,在路由中声明资源或使用:as选项时自动为您设置:
# in routes.rb:
resources :books
# or
get "books/:id" => "books#show", as: :book
# in your view:
book_path(@book, :host => "domain.com")