Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

How do I nest an image_tag inside a link_to?

I want to nest this:

<%= image_tag "logo.svg", id:"logo" %>

inside this:

<%= link_to root_path %>

inside one of my views (it's so users are taken to the home page when they click on my logo).

2 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

This should work:

<%= link_to root_path do %>
  <%= image_tag "logo.svg", id:"logo" %>
<% end %>

Didn't work unfortunately. In Development, the image is there, but nothing happens on hover or click. Strangely, in Production (deployed to Heroku), it shows up as a broken image.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

That is weird, because I just tested this code locally in my own app:

<%= link_to root_path do %>
  <%= image_tag "deleted_user.png", id:"logo"  %>
<% end %>

And it works perfectly fine - displays and goes to root path on click, the html generated by these tags also looks perfectly fine. The image file is stored in the app/assets/images folder.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

If you tell me which branch this is on your github, I can take a look in deployment.

Even weirder is that when I tried it this time, it worked perfectly ;-)

Maybe the mystery is not the code, but my mind!!

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Or your browser is possessed by demons (in one of his videos, Nick Pettit mentioned that this sometimes happens, can't remember which video it was though).