[英]Ruby On Rails image_path
link_to(image_tag(“ icons /#{icon_name} .png”),url_or_object,options)我试图像这样使用它,但是当我进入项目时,我看到的像这样http://prntscr.com/329yzz我看不到图片请帮助我,我也是Ruby的初学者请帮助我
link_to
takes an optional block, if you need anything more complicated than text: 如果您需要比文本更复杂的内容, link_to
带有一个可选块:
link_to url_or_object, options do
image_tag("icons/#{icon_name}.png")
end
Since those method is being used within the views, link_to
is using capture
to evaluate the block. 由于在视图中使用了这些方法,因此link_to
使用capture
来评估块。 Hence it can be used like: 因此可以像这样使用:
<%= link_to url_or_object, options do %>
<div class='wrapper'>
<label>Some text</label>
<%= image_tag("icons/#{icon_name}.png") %>
</div>
<% end %>
You are getting raw html output. 您正在获取原始的html输出。
Use html_safe
as shown below: 使用html_safe
,如下所示:
EDIT As per OP's comments, html_safe
was required in project_title_links
method to convert the link returned from link_to_icon
into an HTML safe string(DOM ready): 编辑根据OP的评论, project_title_links
方法中需要html_safe
才能将从link_to_icon
返回的链接转换为HTML安全字符串(DOM就绪):
def link_to_icon(icon_name, url_or_object,options={})
link_to(image_tag("icons/#{icon_name}.png", ),url_or_object,options)
end
def project_title_links(project)
content_tag :h1 do [project.title, link_to_icon('show',project) ].join(' ').html_safe end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.