如何在带有图像的 link_to 中添加跨度
所以我有这个:
<%= 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
?
So I have this:
<%= link_to(image_tag(@model.picture.url(:thumb), :alt => ''), "/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) %>
Which works, but I need a span in between also like this:
<a id="y_link_2" href="/pages/you/2" class="">
<span>Apples</span>
<img src="another_small.jpg?1236340989" alt="">
</a>
How do I add
<span>Apples</span>
to the link_to
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
向您的 link_to 调用提供一个块:
或者:
Feed a block to your link_to call:
Alternatively:
image_tag
和content_tag
返回基本字符串,因此可以使用+
运算符轻松连接它们:<%= link_to(content_tag( :span, "苹果") + image_tag(@model.picture.url(:thumb), :alt => ''), "/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) %>
但是,正如您所看到的,它变得非常混乱 - 可能值得将其移动到辅助方法中。
image_tag
andcontent_tag
return basic strings, so they can be concatenated using the+
operator easily:<%= link_to(content_tag(:span, "Apples") + image_tag(@model.picture.url(:thumb), :alt => ''), "/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) %>
However, as you can see, it gets quite messy - might be worth moving it into a helper method.
对于路径,请使用如下结构
For a path, use the structure like so
Haml 非常适合此类情况。例如:
或者:
如果你想写下你的观点,值得学习更快并更好地阅读它们。
Haml lends itself well to situations like these. For example:
Alternatively:
It's worth learning if you want to write your views faster and read them better.