[英]undefined method [] for nil:NilClass (NoMethodError) in ruby… Why?
[英]Why is value passed to method in optional hash a `nil`? (Ruby)
我创建了一个自定义方法,并试图通过可选的哈希将值传递给它。
但是错误消息向我表明该方法实际上没有接收到我传递的值,因此为nil
。
视图:
<%= f.custom_form :height, { item: ‘weight’} %>
方法:
def custom_form(type, additional_items = {} )
class_array = [‘form_control’]
if additional_items
class_array << additional_items[:item]
end
end
错误:
没有将nil隐式转换为String
但是我不确定为什么会这样。
如果这是一个新手问题,请提前道歉。
这条线不明确
<%= f.custom_form :height, { item: ‘weight’} %>
因为它可以解释为一个参数和一个块
尝试使用显式括号,并且不需要哈希花括号,我们将假定它们
<%= f.custom_form(:height, item: ‘weight’) %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.