将对象传递给辅助方法

时间:2013-06-14 00:26:07

标签: ruby-on-rails helper

我正在使用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

但这不是:

= link_to_add_fields { name: "Add Skus", f: f, container: "skus", association: :skus, child_association: :images, options: @sku_options }

我收到错误:syntax error, unexpected ':', expecting '}'

帮助方法:

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

1 个答案:

答案 0 :(得分:0)

在你的第二个例子中,Ruby认为你因为括号而传入一个块作为参数。在您的选项周围添加括号,或删除大括号:

= link_to_add_fields({name: "Add Skus", f: f, container: "skus", association: :skus, child_association: :images, options: @sku_options})

= link_to_add_fields name: "Add Skus", f: f, container: "skus", association: :skus, child_association: :images, options: @sku_options