如何在 Ruby on Rails 中获取当前的绝对 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2165665/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:12:56  来源:igfitidea点击:

How do I get the current absolute URL in Ruby on Rails?

ruby-on-railsrubyurl

提问by Jakub Arnold

How can I get the current absolute URLin my Ruby on Rails view?

如何在我的 Ruby on Rails 视图中获取当前的绝对 URL

The request.request_urionly returns the relative URL.

request.request_uri只返回相对URL

回答by Jaime Bellmyer

For Rails 3.2 or Rails 4+

对于 Rails 3.2 或 Rails 4+

You should use request.original_urlto get the current URL.

您应该使用request.original_url来获取当前 URL。

This method is documented at original_url method, but if you're curious, the implementation is:

这个方法记录在original_url method,但如果你很好奇,实现是:

def original_url
  base_url + original_fullpath
end


For Rails 3:

对于 Rails 3:

You can write "#{request.protocol}#{request.host_with_port}#{request.fullpath}", since request.urlis now deprecated.

您可以编写"#{request.protocol}#{request.host_with_port}#{request.fullpath}",因为request.url现在已弃用。



For Rails 2:

对于 Rails 2:

You can write request.urlinstead of request.request_uri. This combines the protocol (usually http://) with the host, and request_uri to give you the full address.

你可以写request.url而不是request.request_uri. 这将协议(通常是 http://)与主机和 request_uri 结合起来为您提供完整地址。

回答by mcr

I think that the Ruby on Rails 3.0 method is now request.fullpath.

我认为 Ruby on Rails 3.0 方法现在是request.fullpath.

回答by grzuy

You could use url_for(only_path: false)

你可以用 url_for(only_path: false)

回答by ecoologic

DEPRECATION WARNING: Using #request_uri is deprecated. Use fullpath instead.

弃用警告:不推荐使用 #request_uri。请改用全路径。

回答by Mike

If you're using Rails 3.2or Rails 4you should use request.original_urlto get the current URL.

如果您使用的是Rails 3.2Rails 4,您应该使用它request.original_url来获取当前 URL。



Documentation for the method is at http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-original_urlbut if you're curious the implementation is:

该方法的文档位于http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-original_url但如果你很好奇,实现是:

def original_url
  base_url + original_fullpath
end

回答by grosser

You can add this current_urlmethod in the ApplicationController to return the current URL and allow merging in other parameters

您可以在 ApplicationController 中添加这个current_url方法来返回当前的 URL 并允许合并其他参数

# https://x.com/y/1?page=1 
# + current_url( :page => 3 )
# = https://x.com/y/1?page=3
def current_url(overwrite={})
    url_for :only_path => false, :params => params.merge(overwrite)
end

Example Usage:

示例用法:

current_url --> http://...
current_url(:page=>4) --> http://...&page=4

回答by Duke

For Ruby on Rails 3:

对于 Ruby on Rails 3:

request.url
request.host_with_port

I fired up a debugger session and queried the request object:

我启动了一个调试器会话并查询了请求对象:

request.public_methods

回答by Lucas Renan

In Ruby on Rails 3.1.0.rc4:

在 Ruby on Rails 3.1.0.rc4 中:

 request.fullpath

回答by Dorian

I needed the application URLbut with the subdirectory. I used:

我需要应用程序URL,但需要子目录。我用了:

root_url(:only_path => false)

回答by Yuri Barbashov

 url_for(params)

And you can easily add some new parameter:

您可以轻松添加一些新参数:

url_for(params.merge(:tag => "lol"))