Ruby-on-rails Rails 3.1 图像的绝对 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7597127/
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
Rails 3.1 absolute URL to an image
提问by alik
I'm using Rails 3.1. I'm trying to figure this out, and to my surprise, it is starting to seem that rails does not come with this method at all. Maybe im wrong.
我正在使用 Rails 3.1。我正在尝试解决这个问题,令我惊讶的是,rails 似乎根本没有提供这种方法。也许我错了。
Can anyone show how I can get a full absolute URL to an image?
谁能展示我如何获得图像的完整绝对 URL?
I use asset_path(image.png)which gives me the relative path to use within the app. I tried doing a root_url + asset_path(image.png)but that just gives me a http://localhost:3000//assets/image.pngwith the double slashes
我使用asset_path(image.png)它给了我在应用程序中使用的相对路径。我试过做一个,root_url + asset_path(image.png)但这只是给了我一个http://localhost:3000//assets/image.png双斜线
Anyone have an efficient way of doing this?
任何人都有这样做的有效方法?
采纳答案by Simone Carletti
See the Using asset hostssection in the documentation. You need to specify an asset_host. You can also construct it dynamically from the request chaining "#{request.protocol}#{request.host_with_port}"
请参阅文档中的使用资产主机部分。您需要指定一个. 您还可以从请求链动态构造它asset_host"#{request.protocol}#{request.host_with_port}"
回答by greggreg
put this in application_helper.rb
把这个放进去 application_helper.rb
def asset_url asset
"#{request.protocol}#{request.host_with_port}#{asset_path(asset)}"
end
then you can use asset_urlin your views.
然后你可以asset_url在你的视图中使用。
回答by Luke W
For Rails 4, and maybe earlier, use:
对于 Rails 4 或者更早版本,使用:
config.action_mailer.asset_host = 'https://assets.com'
回答by Prathan Thananart
In my config/environments/*.rbI already have this tailored for each environment:
在我的config/environments/*.rb我已经为每个环境量身定制了这个:
config.domain = 'mysite.dev'
So it was a simple matter of adding
所以这是一个简单的添加问题
config.action_controller.asset_host = "http://" + config.domain
to each file. Then asset_pathwill miraculously behave as if it were asset_url.
到每个文件。然后asset_path就会奇迹般的表现得好像asset_url。
回答by d.danailov
Example folder structure.
示例文件夹结构。
app/
assets/
flags/
32x32/
en.png
256x256/
en.png
If you want to generate absolute flag image path we can add in to our ApplicationHelper two methods:
如果要生成绝对标志图像路径,我们可以在 ApplicationHelper 中添加两种方法:
module ApplicationHelper
# Generate flag path by locale
# - locale. Can be "en", "it", etc.
# - flag_size. Will be used to set folder size. Folder size can be "32x32", "256x256".
# Return flag image path. Path will absolute
def generate_flag_path_by_locale(locale, folder_size = "32")
folder = "#{flag_size}x#{flag_size}"
domain_absolute_path = generate_domain_absolute_path
flag_path = ("#{domain_absolute_path}/assets/flags/#{folder}/#{locale}.png")
return flag_path
end
# Generate domain absolute path
def generate_domain_absolute_path
request_protocol = request.protocol
request_host_with_port = request.host_with_port
domain_absolute_path = request_protocol + request_host_with_port
return domain_absolute_path
end
end
Into our apps/view/products.html.erb. We must to call only:
进入我们的 apps/view/products.html.erb。我们必须只调用:
<% flag_path = generate_flag_path_by_locale("en") %>
Final result:
最后结果:
回答by Chris Staikos
Could you just do:
你能不能这样做:
root_url[0..-2] + asset_path(image.png)
...to trim the trailing slash in the root url?
...修剪根网址中的尾部斜杠?
回答by HiteshRawal
You need to use 'asset_url' instead *asset_path*.
您需要使用“ asset_url”代替*asset_path*。
Bcz '_path' always return relative path and '_url' will return absolute url.
Bcz '_path' 总是返回相对路径, '_url' 将返回绝对 url。

