1
我正在学习Rails,并且有点卡住了。如果嵌套数组存在,我该如何返回true
我使用简单导航gem并构建自定义渲染器,如果存在配置选项,我想将引导程序图标添加到HTML元素。我收到一个错误“参数数量错误(0代表1)”,我认为这与我的方法有关。我试图将方法定义为:
content_tag('span', (content_tag(:i, nil, :class => item.html_options[:opts][:icon]) + item.name if have_icon?), link_options_for(item).except(:method))
def have_icon?(item)
!item.html_options[:opts][:icon].blank?
end
我不知道我是否正确编写了content_tag。其实我确信我没有。我也认为我得到“错误的参数数量”,因为item.html_options[:opts]
或item.html_options[:opts][:icon]
丢失,因此指出我的方法写得不好。
有人可以帮忙吗?
编辑:
我重写了代码和它的作品,但我的问题仍然有效,所以我可能会在编码更好。所以我的问题是,have_icon?
方法是否正确,或者如果[:opts]
和[:opts][:icon]
都存在,我将如何重写它以返回布尔值?
这是工作代码:
content_tag('span', name_with_icon(item), link_options_for(item).except(:method))
def name_with_icon(item)
if item.html_options[:opts]
if item.html_options[:opts][:icon]
content_tag(:i, nil, :class => item.html_options[:opts][:icon]) + item.name
else
item.name
end
else
item.name
end
end
您的方法需要一个参数。你的电话没有提供。 –
那是显而易见的...谢谢!我不能相信我错过了这一点。 –