126

How do I wrap a link around view code? I can't figure out how to pass multiple lines with ruby code to a single link_to method. The result I am looking for is that you click the column and get the show page:

<div class="subcolumns">
  <div class="c25l">
        <div class="subcl">
        <%= image_tag album.photo.media.url(:thumb), :class => "image" rescue nil  %>
        </div>
    </div>
  <div class="c75r">
        <div class="subcr">
            <p><%= album.created_at %></p>
            <%= link_to h(album.title), album %>
            <p><%= album.created_at %></p>
            <p><%= album.photo_count %></p>
        </div>
  </div>
</div>
Pesto
  • 23,810
  • 2
  • 71
  • 76
atmorell
  • 3,052
  • 6
  • 30
  • 44

5 Answers5

290

link_to takes a block of code ( >= Rails 2.2) which it will use as the body of the tag.

So, you do

<%= link_to(@album) do %>
  html-code-here
<% end %>

But I'm quite sure that to nest a div inside a a tag is not valid HTML.

EDIT: Added = character per Amin Ariana's comment below.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Thorbjørn Hermansen
  • 3,522
  • 1
  • 21
  • 12
  • 19
    This comment is just a reference:
    is valid in HTML5, but not in earlier HTML specs. See http://stackoverflow.com/questions/796087/make-a-div-into-a-link for a similar question.
    – chucknelson Aug 28 '11 at 00:08
  • Rails 2.3.8 still getting "syntax error, unexpected ')'" using the above syntax. – daedelus_j Jun 11 '13 at 15:43
  • If you have a more complex path, you can just add in the parameters, missing the initial content, e.g. <%= link_to some_path, method: :post %> – Obromios Aug 03 '17 at 22:59
14

Also, this may be an issue for some:

Make sure to write <%= if you are doing a simple link with code in it instead of <%.

e.g.

<%= link_to 'some_controller_name/some_get_request' do %>
  Hello World
<% end  %>
Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
sybohy
  • 1,856
  • 2
  • 20
  • 25
9

For older Rails versions, you can use

<% content_tag(:a, :href => foo_path) do %>
  <span>Foo</span>
<% end %>
Omar Qureshi
  • 8,963
  • 3
  • 33
  • 35
7

You can use link_to with a block:

<% link_to(@album) do %>
    <!-- insert html etc here -->
<% end %>
Barry Gallagher
  • 6,156
  • 4
  • 26
  • 30
-3

A bit of a lag on this reply I know -- but I was directed here today, and didn't find a good answer. The following should work:

<% link_to raw(html here), @album %>
Rob Dawson
  • 1,349
  • 10
  • 21
  • 5
    This shouldn't be used as all html entered inside the `raw` is prone to XSS. – Aurril Dec 19 '11 at 05:40
  • Not necessarily, it might be the case that the HTML is being generated from somewhere within your own app which you know to be safe. Still, it's best to avoid this in 99% of situations. (Not to mention the above code has a mistake anyway, it should start with `<%=`, not `<%`.) – GMA Oct 24 '13 at 07:11
  • Not the best way. html here would have to be generated by your own app and ensured to be safe. There are other better ways in answers above. – Joshua Dance Feb 19 '15 at 20:50