Rails:如何分配链接到图像
问题描述:
我需要在页面中链接图像。我的代码是:Rails:如何分配链接到图像
<%= image_tag p.photos.first.avatar.url(:small) if p.photos.size > 0 %>
如何使此链接?
试着这样做:
<%= link_to (image_tag p.photos.first.avatar.url(:small) if p.photos.size > 0), product_path(p.id) %>
但给人语法错误。
任何一个?
答
变化的链路使用块形式(为了更好的可读性)
<%= link_to product_path(p.id) do %>
<% if p.photos.any? %>
<%= image_tag(p.photos.first.avatar.url(:small)) %>
<% else %>
<div>Default text for the link if the image is not present</div>
<% end %>
<% end %>
答
<%= link_to image_tag(p.photos.first.avatar.url(:small)).html_safe, product_path(p.id) if p.photos.size > 0%>
+0
不工作..不显示图像。 – user1977201 2013-03-11 09:28:24
这真的很棒。我现在做轨道2年,但我不知道link_to可以处理块^^ – davidb 2013-03-11 08:59:15
是的,我们都学习新的东西每天:) – jvnill 2013-03-11 09:01:35
哇!像魅力一样工作。非常感谢。正在寻找解决方案,从很多天。 :-) – user1977201 2013-03-11 09:29:09