我有部分:
<% if title_top.any? %>
<% title_top.each do |country| %>
<li><%= link_to country.title, country%></li>
<% end %>
<li class="divider"></li>
<% end %>
它工作正常并生成带有链接的列表标签,但我想用助手替换它:
def link_to_list(var)
if var.any?
var.each do |country|
content_tag :li do
link_to(country.title, country)
if var.first.top?
content_tag(:li, class: "divider")
end
end
end
end
end
参数的国家/地区数组:
def title_top
@country_top = Country.where(top: true)
end
助手不起作用,在控制台中它给了我:
ArgumentError: arguments passed to url_for can't be handled. Please require routes or provide your own implementation
我错了,请帮助...我可以部分保留,但我的大脑很快就会爆炸,因为我找不到正确的解决方案
谢谢 juanpastas,对我来说正确的答案是:
def link_to_list(var)
out = ''
devider ="<li class='divider'></li>" #divider for bootstrap menu
var.each do |country|
out += content_tag :li do #list item with links inside
link_to country.title, country
end
end
var.first.top ? (out << devider).html_safe : out.html_safe #divide upper menu links from other links
end
(但我仍然不明白为什么以前的方法不起作用)