I have been searching and searching for this answer. But I cannot seem to make this work, several hours now. Please please help me.
我一直在寻找并寻找这个答案。但是现在好几个小时我似乎无法完成这项工作。请帮帮我。
I'm a webpage of my rails app, I am trying to show an image saved in my assets folder @app/assets/images/rails.png.
我是我的rails应用程序的网页,我正在尝试显示保存在我的资源文件夹@ app / assets / images / rails.png中的图像。
I have a javascript file with the following function that constructs html. Inside this function, I'd like to pass the link to the image. This is the code I have currently.
我有一个javascript文件,其中包含构造html的以下函数。在这个函数里面,我想把链接传递给图像。这是我目前的代码。
function addToInfoWindow(infoWindowContent)
{
infoWindowString = '<div class="infoWindow">'+
**'<img src="/assets/images/rails.png" />'+**
'<p>'+infoWindowContent+'</p>'+
'<p><a href="http://www.google.com">See here</a></p>'+
'<p><a href="http://www.google.com">Upload a photo</a></p>'+
'</div>';
infoWindow.setContent(infoWindowString);
}
As you can see above in the code the bold part is the problem. I've tried several different combinations of url strings to access that image file but the image does not show up in the html element.
正如您在代码中看到的那样,粗体部分就是问题所在。我已经尝试了几种不同的url字符串组合来访问该图像文件,但图像没有显示在html元素中。
I've looked and looked, tried rails helper functions such as image_url('rails.png')
. But I must be putting them in the wrong place. Can someone please help me. Please show me where , what function in the above code I need to add to fetch the image at /assets/images/rails.png
so that it's url is placed in the highlighted part above and it shows in my view.
我看了看,试过rails帮助函数,比如image_url('rails.png')。但我必须把它们放在错误的地方。有人可以帮帮我吗。请告诉我上面代码中我需要添加什么函数来获取/assets/images/rails.png中的图像,以便它的url被放置在上面突出显示的部分中,并显示在我的视图中。
2 个解决方案
#1
23
If you want to you use the rails helpers you need to "upgrade" your javascript files to erb.
如果你想使用rails helper,你需要将你的javascript文件“升级”为erb。
Just rename them from whatever.js
to whatever.js.erb
. Now rails will execute all the erb code (stuff between <%= and %>) before delivering the file.
只需将它们从whatever.js重命名为whatever.js.erb即可。现在rails将在传递文件之前执行所有erb代码(<%=和%>之间的东西)。
So you can do like this:
所以你可以这样做:
<img src="<%= asset_path( 'rails.png' ) %>" />
More information, see 2.2.3.
更多信息,请参见2.2.3。
#2
3
You won't be able to use the rails helpers like image_tag in pure javascript - append .erb and use asset_path (see klump's answer)
您将无法在纯javascript中使用像image_tag这样的rails助手 - 追加.erb并使用asset_path(请参阅klump的回答)
#1
23
If you want to you use the rails helpers you need to "upgrade" your javascript files to erb.
如果你想使用rails helper,你需要将你的javascript文件“升级”为erb。
Just rename them from whatever.js
to whatever.js.erb
. Now rails will execute all the erb code (stuff between <%= and %>) before delivering the file.
只需将它们从whatever.js重命名为whatever.js.erb即可。现在rails将在传递文件之前执行所有erb代码(<%=和%>之间的东西)。
So you can do like this:
所以你可以这样做:
<img src="<%= asset_path( 'rails.png' ) %>" />
More information, see 2.2.3.
更多信息,请参见2.2.3。
#2
3
You won't be able to use the rails helpers like image_tag in pure javascript - append .erb and use asset_path (see klump's answer)
您将无法在纯javascript中使用像image_tag这样的rails助手 - 追加.erb并使用asset_path(请参阅klump的回答)