简体   繁体   English

如何围绕某些html ruby​​代码包装link_to?

[英]How do I wrap link_to around some html ruby code?

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. 我无法弄清楚如何将多行使用ruby代码传递给单个link_to方法。 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>

link_to takes a block of code ( >= Rails 2.2) which it will use as the body of the tag. link_to 获取一段代码 (> = Rails 2.2),它将用作标记的主体。

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. 我很确定在a标签内嵌入div是无效的HTML。

EDIT: Added = character per Amin Ariana's comment below. 编辑:已添加=每个Amin Ariana的评论下面的评论。

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 <% . 如果您正在使用代码中的代码而不是<%请确保编写<%=

eg 例如

<%= link_to 'some_controller_name/some_get_request' do %>
  Hello World
<% end  %>

For older Rails versions, you can use 对于较旧的Rails版本,您可以使用

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

You can use link_to with a block: 您可以将link_to与块一起使用:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM