如何将 link_to 包裹在一些 html ruby​​ 代码中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1086360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:26:06  来源:igfitidea点击:

How do I wrap link_to around some html ruby code?

ruby-on-railsruby

提问by atmorell

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_tomethod. The result I am looking for is that you click the column and get the show page:

如何在视图代码周围环绕链接?我不知道如何将带有 ruby​​ 代码的多行传递给一个link_to方法。我正在寻找的结果是您单击该列并获得显示页面:

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

回答by Thorbj?rn Hermansen

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

ButI'm quite sure that to nest a divinside a atag is not valid HTML.

我很确定diva标签内嵌套 a不是有效的 HTML。

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

编辑:=根据 Amin Ariana 下面的评论添加了角色。

回答by sybohy

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

回答by Omar Qureshi

For older Rails versions, you can use

对于较旧的 Rails 版本,您可以使用

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

回答by Barry Gallagher

You can use link_towith a block:

您可以link_to与块一起使用:

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

回答by Rob Dawson

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