我有一个页脚助手来显示链接
def footer_helper
resources = ["tweet","questions"] # and perhaps something more
resources.map do |resource|
if current_page?(controller: resource.pluralize, action: 'index')
link_to "New #{resource.humanize}", {controller: resource.pluralize, action: 'new'}
else
link_to "#{resource.pluralize.humanize}",{controller: resource.pluralize, action: 'index'}
end
end.join(" ")
end在footer.html.erb中,我写道:
<%= raw footer_helper %>问题:
.join(" ")很丑。有更好的语法吗?如果我不使用它,.map将返回一个包含链接html的数组。发布于 2013-12-18 14:46:14
def footer_helper
["tweet","questions"].map do |resource|
if current_page?(controller: resource.pluralize, action: 'index')
link_to "New #{resource.humanize", send(:"new_#{resource}_path")
else
link_to resource.pluralize.humanize, send(:"index_#{resource}_path")
end
end.join(" ").html_safe
end在footer.html.erb中,简单地说:
<%= footer_helper %>发布于 2013-12-18 14:39:17
我认为连接很好,但是您正在实例化一个不需要的变量。此外,您还可以使用send作为路径。代码在下面。
def footer_helper
["tweet","questions"].map do |resource|
if current_page?(controller: resource.pluralize, action: 'index')
link_to "New #{resource.humanize}", send("new_#{resource}_path".to_sym)
else
link_to "#{resource.pluralize.humanize}", send("index_#{resource}_path".to_sym)
end
end.join(' ')
endhttps://stackoverflow.com/questions/20660942
复制相似问题