Search code examples
ruby-on-railslink-tocontent-tag

Chaining link_to content_tag Image_tag in Rails


I have the following code:

<%= link_to(content_tag(image_tag('img_blank.png', alt: "Continue"),:div, [class: "btn", id: "continue"])) %>

however, I am getting the following error:

undefined method `each_pair' for [{:class=>"btn", :id=>"continue"}]:Array

Is it possible to chain erb tags like this? What am I missing?


Solution

  • You should write the code for easier reading:

    <%= link_to("/url") do %>
      <%= content_tag(:div, class: "btn", id: "continue") do %>
        <%= image_tag('img_blank.png', alt: "Continue") %>
      <% end %>
    <% end %>
    

    Output:

    <a href="/url">
      <div class="btn" id="continue">
        <img alt="Continue" src="/images/img_blank.png">
      </div>
    </a>