Mike McCallen
Mike McCallen

Reputation: 307

How to attach an URL to an image

I tried different ways to display a picture in my rails app,and when the user clicks on that he should be redirected to the URL i specify.

I tried this method,"rails method"

<li <%= link_to new_gig_path %><%= image_tag 'v.png' %></li>

and just the image was showing,but the image wasn't clickable.

than i tried this"HTML method" <a href="the redirect url"><img src="link to an image"/></a>

And it worked as i wanted,the image appeared,and when i clicked on it,it redirected where i wanted.The problem is that it is wrapped in <a></a> tags,but i need the code to be wrapped in <li></li> tags,

note:I also tried to wrap the <a></a> tags in <li></li> tags but it destroyed my design,i do need it to be just in <li> tags

looking into my navigation bar,you see that my nav-bar is structured in <li>tags

#instead of this comment I want to put the code,i am asking help with
<li><%= link_to '+Add Box', new_gig_path, id: "addgig" %></li>
<li><%= link_to 'Edit Profile',edit_user_registration_path, id: "menu-overwritten" %></li>
<li><%= link_to "Log out", destroy_user_session_path, :method => :delete, id: "menu-overwritten"%></li>

More info: Also i tried this method

<li><a href="url link to redirect"><img src="a link to an image" height="30" width="159" ></a></li>

This time it worked as well,but i don't think it is gonna be responsive if i leave this method.This is definitely not the rails way. Thank you.

Upvotes: 0

Views: 271

Answers (1)

Antarr Byrd
Antarr Byrd

Reputation: 26061

<li>
  <%= link_to new_gig_path  do %>
    <%= image_tag 'v.png', class: 'your-class' %>
  <% end %>
</li>

Upvotes: 1

Related Questions