简体   繁体   English

将对象传递给辅助方法

[英]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.

相关问题 如何将对象从form_for助手传递给方法? - How can you pass an object from the form_for helper to a method? Rails-将选项传递到接受块的辅助方法中? - Rails - Pass options into a helper method that accepts a block? activeadmin下拉菜单将值与帮助程序方法一起传递给集合 - activeadmin dropdown pass values with helper method to collection 我可以在服务对象中包含帮助方法吗? - Can I include helper method in service object? Ruby on Rails,带有对象方法的表单助手 - Ruby on Rails, form helper with object method Rails 助手在 self 方法中将负号作为参数传递 - Rails helper pass negative symbol as argument in self method 如何在导轨 URL 助手中传递 object - How do I pass an object in the rails URL helper 在Rails中,所有方法都在View帮助器方法中吗? View内的任何方法都不是辅助方法? (其他对象的方法调用除外) - In Rails, are all the method inside of View helper methods? Any method inside View is not helper method? (except other object's method invocation) 如何使一个辅助方法来检查对象的存在状态? - How to make a helper method to check existance status of an object? Rails Test辅助方法接受ActionView :: Helpers :: FormBuilder对象 - Rails Test helper method accepting ActionView::Helpers::FormBuilder object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM