Add image to layout in ruby on rails

2019-01-21 05:23发布

I would like to add an image in my template for my ruby on rails project where i currenly have the code <img src="../../../public/images/rss.jpg" alt="rss feed" /> in a the layout stores.html.erb file however this doesn't seem to load as it looks like its missing a route which i'm not sure what its supposed to be.

Any ideas please?

6条回答
相关推荐>>
2楼-- · 2019-01-21 05:46

When using the new ruby, the image folder will go to asset folder on folder app

after placing your images in image folder, use

<%=image_tag("example_image.png", alt: "Example Image")%>
查看更多
我只想做你的唯一
3楼-- · 2019-01-21 05:49

simple just use the img tag helper. Rails knows to look in the images folder in the asset pipeline, you can use it like this

<%= image_tag "image.jpg" %>
查看更多
一夜七次
4楼-- · 2019-01-21 05:53

In a Ruby on Rails project by default the root of the HTML source for the server is the public directory. So your link would be:

<img src="images/rss.jpg" alt="rss feed" />

But it is best practice in a Rails project to use the built in helper:

<%= image_tag("rss.jpg", :alt => "rss feed") %>

That will create the correct image link plus if you ever add assert servers, etc it will work with those.

查看更多
做个烂人
5楼-- · 2019-01-21 05:53

It's working for me:

<%= image_tag( root_url + "images/rss.jpg", size: "50x50", :alt => "rss feed") -%>

查看更多
趁早两清
6楼-- · 2019-01-21 06:01

image_tag is the best way to do the job friend

查看更多
The star\"
7楼-- · 2019-01-21 06:12

Anything in the public folder is accessible at the root path (/) so change your img tag to read:

<img src="/images/rss.jpg" alt="rss feed" />

If you wanted to use a rails tag, use this:

<%= image_tag("rss.jpg", :alt => "rss feed") %>
查看更多
登录 后发表回答