raisl
raisl

Reputation: 11

Rails Image_Tag not Accessing Images in other Directories

As of day three of trying to get this to work, I'm wondering if there are special rules involving the image_tag and how it works in a rails context. My rails html tag is as follows on the application.html.erb in the layout directory. I will get to the paths in a minute.

<%=image_tag('./app/assets/images/leaf.jpg')%>

The directory path of the image is:

app/assets/images/leaf.jpg

The directory path of the file is:

app/views/layouts/application.html.erb

I'm thinking that views is within app and just has to get back out of layouts to access app/assets/images....etc.

Upvotes: 1

Views: 124

Answers (1)

fool-dev
fool-dev

Reputation: 7777

You can just use this, in regular views, you can access images in the app/assets/images directory like this:

<%= image_tag('leaf.jpg') %>

See the Rails Guides what say about asset pipeline.

CSS and ERB

The asset pipeline automatically evaluates ERB. This means if you add an erb extension to a CSS asset (for example, application.css.erb), then helpers like asset_path are available in your CSS rules:

.class { background-image: url(<%= asset_path 'leaf.jpg' %>) }

Upvotes: 1

Related Questions