Ruby-on-rails Rails 中带有 url_for 的完整 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3489838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Full URL with url_for in Rails
提问by Victor
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!)
谢谢(如果答案很明显,我很抱歉。我正在学习 Rails!)
采纳答案by venables
Use the :host option. For example, you can use:
使用 :host 选项。例如,您可以使用:
url_for(@book, :host => "domain.com")
Note: with Rails 3 and above, use polymorphic_urlinstead of url_for.
注意:对于 Rails 3 及更高版本,使用polymorphic_url代替url_for。
回答by Andy Gaskell
According to the docs, this shouldn't happen. The option you're looking for is :only_pathand it's false by default. What happens if you set it to false explicitly?
根据文档,这不应该发生。您正在寻找的选项是:only_path,默认情况下它是假的。如果您将其显式设置为 false 会发生什么?
url_for(@book, :only_path => false)
While you can use url_foryou 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)路径。
回答by Ryan Bigg
If it's a RESTful resource you'll be able to use this:
如果它是 RESTful 资源,您将能够使用它:
book_url(@book)
回答by a moose
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")
回答by sandre89
In Rails 5, if you want the full url for the current controller/action (== current page), just use:
在 Rails 5 中,如果您想要当前控制器/操作(== 当前页面)的完整 url,只需使用:
url_for(only_path: false)
Long answer:
长答案:
In Rails 5, url_forin a view is ActionView::RoutingUrlFor#url_for. If you look at it's source code (https://api.rubyonrails.org/classes/ActionView/RoutingUrlFor.html#method-i-url_for), you'll see if you pass a Hash (keyword parameters are cast into a Hash by Ruby), it actually calls super, thus invoking the method of same name in it's ancestor.
在 Rails 5 中,url_for视图是ActionView::RoutingUrlFor#url_for. 如果您查看它的源代码 ( https://api.rubyonrails.org/classes/ActionView/RoutingUrlFor.html#method-i-url_for),您将看到是否传递了 Hash(关键字参数被 Ruby 转换为 Hash),它实际上调用了super,从而调用了它的祖先中的同名方法。
ActionView::RoutingUrlFor.ancestorsreveals that it's first ancestor is ActionDispatch::Routing::UrlFor.
ActionView::RoutingUrlFor.ancestors揭示它的第一个祖先是ActionDispatch::Routing::UrlFor。
Checking it's source code (https://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html#method-i-url_for), you'll read this:
检查它的源代码(https://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html#method-i-url_for),你会读到:
Missing routes keys may be filled in from the current request's parameters (e.g. :controller, :action, :id and any other parameters that are placed in the path).
可以从当前请求的参数(例如:controller、:action、:id 和放置在路径中的任何其他参数)中填充缺少的路由键。
This is very nice, since it will build automatically a URL for you for the current page (or path, if you just invoke url_forwithout the only_path: false). It will also intelligently ignore the query string params; if you need to merge those, you can use url_for(request.params.merge({arbitrary_argument:'value'})).
这非常好,因为它会自动为您构建当前页面的 URL(或路径,如果您只是在url_for没有 的情况下调用only_path: false)。它还会智能地忽略查询字符串参数;如果您需要合并这些,您可以使用url_for(request.params.merge({arbitrary_argument:'value'})).

