4

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 ?

Geoffrey H
  • 1,280
  • 2
  • 10
  • 33

1 Answers1

3

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.

mikej
  • 65,295
  • 17
  • 152
  • 131
  • Just tried but it sill renders outside the link tag :/ I really thought this would work when I saw your answer, this really odd – Geoffrey H Sep 20 '12 at 14:15
  • Even when I paste the partials content directly in my `<%= link_to(result) do %>` block, it renders outside – Geoffrey H Sep 20 '12 at 14:19
  • 1
    Does sound odd. But from your second comment sounds like the issue is not related to using the partial then? Does `<%= link_to(result) do %>very simple link<% end %>` work correctly? – mikej Sep 20 '12 at 14:22
  • Yes, I've tried `<%= link_to(result) do %>test<% end %>` and `<%= link_to(result) do %>
    TEST
    <% end %>`. They both work.
    – Geoffrey H Sep 20 '12 at 14:28
  • 1
    Just guessing now, but could it be the nesting of another block `result.nbetoile.times do`? .. just spotted you also have a `link_to` within your partial so wouldn't that result in a link within a link as well? – mikej Sep 20 '12 at 14:31
  • Yes this is it ! It breaks when I put it back in. But I really need this block so I can put the right number of stars the camping has... – Geoffrey H Sep 20 '12 at 14:36
  • 2
    The `*` operator can be used to repeat a string in Ruby so you should be able to get the right number of stars with just `<%= image_tag("/assets/star.png", class: "star") * result.nbetoile %>` without needing a block. – mikej Sep 20 '12 at 14:40
  • Thanks, this is very useful. Although, it turns out that now that I'm calling the partial again, things start to break again even without the block. Links are correct when the partial only contains `<%= result %>` but it seems as soon as there is a new call to link_to things start to go crazy... I get empty links that shouldn't be here and the partial gets out the link tag again – Geoffrey H Sep 20 '12 at 14:47
  • Also there seems to be no difference between render with `<% %>` or `<%= %>`, which is odd too.. – Geoffrey H Sep 20 '12 at 14:52
  • Ok, so I think I know what's really causing the problem, it's not the `link_to` function, it's the presence of a link tag in the partial. It works whenever there are no links present, but as soon as I had even a `` it breaks – Geoffrey H Sep 20 '12 at 15:16
  • Just a quick update, I've now tried this myself in 3.2.8 and it seems to work fine for me. i.e. a link within the partial doesn't cause any problems. Have you got any plugins in your app that could be interfering? Have you tried it in a new, minimal app? – mikej Sep 22 '12 at 22:19