Geoffrey H
Geoffrey H

Reputation: 1268

Why can't I nest a partial in a link_to block in Rails?

I've seen many posts about this and even the documentation says that if you do

<%= link_to(result) do %>
    html-content-here
<% end %>

Then you get an a tag with your html content inside

However, when I do this

<%= link_to(result) do %>
    <%= render :partial => 'campings/inSelection', :locals => { :result => result } %>
<% end %>

My link and partial are present, but I get something like this instead

<a href="link"></a>
partial's-content-here

Which is obviously not what I want, is this impossible ?

This is my partial's content :

  <div class="campingInHighlights">
      <% imgUrl = result.photo1.blank? ? "/assets/camping_defaut.png" : "http://applicampings.francecom.com/photos/fiches/liste/#{result.photo1}"  %>
      <%= image_tag imgUrl, class:"photo" %>
      <h3><%= link_to result.nom.to_s.downcase.capitalize, result %></h3>
      <p><%= result.ville.to_s.downcase.capitalize %> (<%= result.departement.code %>)</p>
      <div class="stars">
         <% result.nbetoile.times do %>
             <%= image_tag "/assets/star.png", class: "star" %>
         <% end %>
      </div>
  </div>

EDIT : So it turns out the problem is because of the link tags contained in my partial, there can not be any of these into the partial, whether they're created manually or via the link_to function. Can someone please explain what's going on here ?

Upvotes: 4

Views: 521

Answers (1)

mikej
mikej

Reputation: 66263

Try replacing the <%= render.. tag with a <% render.. (no =) so that the render block returns the text of the partial but does not itself output it.

Upvotes: 3

Related Questions