Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add image to layout in ruby on rails

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?

like image 658
Erika Avatar asked Nov 29 '09 05:11

Erika


People also ask

What is the layout in Ruby on Rails?

A layout defines the surroundings of an HTML page. It's the place to define a common look and feel of your final output. Layout files reside in app/views/layouts. The process involves defining a layout template and then letting the controller know that it exists and to use it.

How should you use nested layouts in rails?

Rails provides us great functionality for managing layouts in a web application. The layouts removes code duplication in view layer. You are able to slice all your application pages to blocks such as header, footer, sidebar, body and etc.

How can you tell Rails to render without a layout *?

By default, if you use the :text option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option.


2 Answers

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") %> 
like image 152
Doug Neiner Avatar answered Sep 21 '22 18:09

Doug Neiner


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.

like image 31
scottd Avatar answered Sep 21 '22 18:09

scottd