我在rails助手文件中有这样一个方法
def table_for(collection, *args)
options = args.extract_options!
...
end
我希望能够像这样调用这个方法
args = [:name, :description, :start_date, :end_date]
table_for(@things, args)
这样我就可以基于表单提交动态地传递参数.我不能重写这个方法,因为我在太多地方使用它,否则我怎么做呢?
我在rails助手文件中有这样一个方法
def table_for(collection, *args)
options = args.extract_options!
...
end
我希望能够像这样调用这个方法
args = [:name, :description, :start_date, :end_date]
table_for(@things, args)
这样我就可以基于表单提交动态地传递参数.我不能重写这个方法,因为我在太多地方使用它,否则我怎么做呢?
Ruby可以很好地处理多个参数.
Here is是一个很好的例子.
def table_for(collection, *args)
p collection: collection, args: args
end
table_for("one")
#=> {:collection=>"one", :args=>[]}
table_for("one", "two")
#=> {:collection=>"one", :args=>["two"]}
table_for "one", "two", "three"
#=> {:collection=>"one", :args=>["two", "three"]}
table_for("one", "two", "three")
#=> {:collection=>"one", :args=>["two", "three"]}
table_for("one", ["two", "three"])
#=> {:collection=>"one", :args=>[["two", "three"]]}
(从irb剪切和粘贴的输出)