[英]Pass object into helper method
I'm using rails 3.2.11 and trying to pass an object into a custom helper from a view and I don't understand why this works: 我正在使用Rails 3.2.11并试图从视图中将对象传递到自定义帮助器中,但我不明白为什么会这样:
- @data = { name: "Add Skus", f: f, container: "skus", association: :skus, child_association: :images, options: @sku_options }
= link_to_add_fields @data
but this doesn't: 但这不是:
= link_to_add_fields { name: "Add Skus", f: f, container: "skus", association: :skus, child_association: :images, options: @sku_options }
I get the error: syntax error, unexpected ':', expecting '}'
我收到错误消息:
syntax error, unexpected ':', expecting '}'
Helper method: 辅助方法:
def link_to_add_fields(data)
STDOUT << "name: " + data[:name].to_s + " :: "
STDOUT << "f.object: " + data[:f].object.to_s + " :: "
STDOUT << "association: " + data[:association].to_s + " :: "
STDOUT << "container: " + data[:container].to_s + " :: "
if data[:child_association]
STDOUT << "child_association: " + data[:child_association].to_s + " :: "
end
end
In your second example, Ruby thinks you're passing in a block as a parameter due to the braces. 在第二个示例中,Ruby认为由于括号,您正在将块作为参数传递。 Add parentheses around your options, or get rid of the braces:
在选项周围加上括号,或删除括号:
= link_to_add_fields({name: "Add Skus", f: f, container: "skus", association: :skus, child_association: :images, options: @sku_options})
or 要么
= link_to_add_fields name: "Add Skus", f: f, container: "skus", association: :skus, child_association: :images, options: @sku_options
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.