Search code examples
ruby-on-railslink-to

Use an image path from database as link_to


I have an image being pulled from the database in a table, that I would then like to make into a link so the viewer can be taken to the full article. I have seen that in theory I could use the below,

<%= link_to image_tag('image/someimage.png') + "Some text", some_path %>

but in my case 'image/someimage.png' is

<%= image_tag coffeeshop.image_thumb_path %>

I've tried simply dropping the image tag section in, so it becomes <%= link_to image_tag('image_tag coffeeshop.image_thumb_path') + "Some text", some_path %> but this doesn't work. Is there a way to do this?


Solution

  • link_to(body, url, html_options = {})
    

    The first argument in your link_to will be interpreted as the body of the tag, and then your url, but if you want to add more content inside the link_to you can open it and then close it, all inside will be interpreted this way:

    <a href="#">
      ...
    </a>
    

    So you can try:

    <%= link_to some_path do %>
      <%= image_tag coffeeshop.image_thumb_path %>
    <% end %>