简体   繁体   English

如何在Rails 3中获取资源的绝对路径?

[英]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.

相关问题 如何在Rails中获取资产文件夹的绝对路径? - How to get the absolute path of assets folder in rails? 如何在 Ruby on Rails 中获取当前绝对 URL? - How do I get the current absolute URL in Ruby on Rails? 如何在 Rails 3.1 中获得绝对的 URL 资产? - How do I get an absolute URL for an asset in Rails 3.1? 如何为包括助手的资源创建单个GET路径? - How do I create a singular GET path for a resource including helpers? Ruby on Rails中的“不良URI(绝对但没有路径):http://”错误是什么意思,我该如何防止它? - What does the “bad URI(absolute but no path): http://” error in ruby on rails mean and how do I prevent it? 如何获得Rails 4中redirect_to给定的等效路径? - How do I get the equivalent path given by redirect_to in Rails 4? 在Rails应用程序中,如何获取路径的路线? - How do I get the route for a path, in a rails application? 在Rails 3中,如何获取控制器的“ index”动作的路径? - In Rails 3, how do I get the path of a controller's `index` action? 如何在Rails 3.1中为作用域/嵌套资源创建named_route_path? - How do I create a named_route_path for a scoped/nested resource in rails 3.1? 如何在Rails 3中用命名路由替换资源路径? - How can I substitute a named route for a resource path in Rails 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM