所以我有这个:
<%= link_to(image_tag(@model.picture.url(:thumb), :alt => ''), "/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) %>
哪个有效,但我需要介于两者之间,如下所示:
<a id="y_link_2" href="/pages/you/2" class="">
<span>Apples</span>
<img src="another_small.jpg?1236340989" alt="">
</a>
如何添加
<span>Apples</span>
到link_to
?
答案 0 :(得分:17)
将一个块提供给您的link_to电话:
<% link_to("/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) do %>
<%= content_tag(:span, 'Apples') %>
<%= image_tag(@model.picture.url(:thumb), :alt => '') %>
<% end %>
可替换地:
<% link_to("/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) do %>
<span>Apples</span>
<%= image_tag(@model.picture.url(:thumb), :alt => '') %>
<% end %>
答案 1 :(得分:12)
image_tag
和content_tag
返回基本字符串,因此可以轻松地使用+
运算符连接它们:
<%= link_to(content_tag(:span, "Apples") + image_tag(@model.picture.url(:thumb), :alt => ''), "/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) %>
但是,正如您所看到的,它变得非常混乱 - 可能值得将其转换为辅助方法。
答案 2 :(得分:0)
Haml很适合这些情况。例如:
= link_to "/pages/you/#{something.id}", id: "y_link_#{something.id} do
%span Apples
= image_tag(@model.picture.url(:thumb), alt: '')
%a[something, 'y_link']{href: "/pages/you/#{something.id}"}
%span Apples
%img{src: @model.picture.url(:thumb)}
如果你想更快地写下你的观点并更好地阅读它们,那么值得学习。
答案 3 :(得分:0)
对于路径,请使用类似的结构
<%= link_to edit_section_path(@section) do %>
Edit
<span class="fa fa-list pull-right"></span>
<% end %>