[英]How do I get the absolute path of a resource in Rails 3?
I would like to get the full absolute path to a resource including the domain name in Rails 3. 我想获得一个资源的完整绝对路径,包括Rails 3中的域名。
Example: to get the full path to the home page I have tried: 示例:要获取我尝试过的主页的完整路径:
url_for( :controller => 'home', :action => 'index' )
and 和
root_path
but both give me just: /
but I want: http://www.example.com/
但两个都只给我:
/
但我想要: http://www.example.com/
: http://www.example.com/
The answer should also work on my dev server and return: http://localhost:3000/
答案也应该在我的开发服务器上运行并返回:
http://localhost:3000/
Use root_url
. 使用
root_url
。 The _url
extension on the end gives the full URL, something like http://localhost:3000/projects/1/tickets/2
. 最后的
_url
扩展名提供了完整的URL,例如http://localhost:3000/projects/1/tickets/2
。
Ryan's answer is perfectly correct. 瑞安的回答是完全正确的。
However, if you really needed to use url_for
someday, just add :only_path => false
to it's options and it'll make the url absolute for you. 但是,如果有一天你真的需要使用
url_for
,只需在它的选项中添加:only_path => false
,它就会为你创建绝对的url。 ;) ;)
If you want the resouce url (instead of only the root url). 如果你想要资源网址(而不是只有根网址)。 You have to use resoure_name_url(@resource_instance)
你必须使用resoure_name_url(@resource_instance)
Example: 例:
resource_url(@resource_instance)
Or if you have (for example) the users resource: 或者,如果您有(例如)用户资源:
user_url(@user)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.