Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在rails中的对象数组上使用collection_单选按钮_Ruby On Rails_Ruby_Ruby On Rails 4_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails 在rails中的对象数组上使用collection_单选按钮

Ruby on rails 在rails中的对象数组上使用collection_单选按钮,ruby-on-rails,ruby,ruby-on-rails-4,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 4,Ruby On Rails 5,我有一个对象数组,我希望能够使用collection_单选按钮显示它们,但collection_单选按钮似乎只适用于数组或ActiveRecord对象。是否有任何方法可以使用对象数组构建集合单选按钮 这是我的对象数组 [{:image_url=>"https://assets.braintreegateway.com/payment_method_logo/visa.png?environment=sandbox", :card_type=>"Visa", :last_4=>"

我有一个对象数组,我希望能够使用collection_单选按钮显示它们,但collection_单选按钮似乎只适用于数组或ActiveRecord对象。是否有任何方法可以使用对象数组构建集合单选按钮

这是我的对象数组

[{:image_url=>"https://assets.braintreegateway.com/payment_method_logo/visa.png?environment=sandbox", :card_type=>"Visa", :last_4=>"0004", :expiration_month=>"10", :expiration_year=>"2019"}, {:image_url=>"https://assets.braintreegateway.com/payment_method_logo/visa.png?environment=sandbox", :card_type=>"Visa", :last_4=>"0004", :expiration_month=>"11", :expiration_year=>"2019"}]
这是我试图编写的代码,但它不起作用

<%= collection_radio_buttons("cards", :card, @payment_methods, item_wrapper_tag: false) do |b|
   b.label { b[:last_4]}
end %>

欢迎任何帮助

帮助者正在寻找一组对象,这些对象将响应
text\u方法
输入,然后用于生成单选按钮标签

让代码工作的快捷方法是将哈希数组转换为响应以键命名的方法的对象数组,即我们可以使用以下方法将它们转换为OpenStructs:

@payment_methods=@payment_methods.map{| pm | OpenStruct.new(pm)}
传递这个新的OpenStructs数组现在应该打印出您要查找的内容,而不需要额外的块:


您可能希望/需要更改其他输入-
:card
:cards
:last_4
-以获得与正在构建的表单一起使用的所需结果

另一个选择是自己构建单选按钮,而不使用内置的
collection\u单选按钮
helper。。。比如: