127

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>

5 Answers 5

293

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This comment is just a reference: <a><div></div></a> is valid in HTML5, but not in earlier HTML specs. See stackoverflow.com/questions/796087/make-a-div-into-a-link for a similar question.
Rails 2.3.8 still getting "syntax error, unexpected ')'" using the above syntax.
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 %>
15

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  %>

1 Comment

I think this is required in Rails 3+
9

For older Rails versions, you can use

<% content_tag(:a, :href => foo_path) do %>
  <span>Foo</span>
<% end %>

Comments

7

You can use link_to with a block:

<% link_to(@album) do %>
    <!-- insert html etc here -->
<% end %>

1 Comment

link_to requires '=' to show up (even with a block)
-4

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 %>

4 Comments

This shouldn't be used as all html entered inside the raw is prone to XSS.
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 <%.)
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.
definitely worst answer on here. Do not do this!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.