简体   繁体   English

从红宝石数组中填充下拉列表

[英]populate drop down list from array in ruby

I have a country list that I have called in - and have parsed into the following format: 我有一个打电话给的国家/地区列表,并且已解析为以下格式:

{"Countries"=>[{"Name"=>"ABKHAZIA", "IsoCode"=>"AB", "HasTown"=>"I"}, {"Name"=>"ANGUILLA", "IsoCode"=>"AI", "HasTown"=>"I"}, {"Name"=>"ANTIGUA", "IsoCode"=>"AG", "HasTown"=>"I"}, .... {"Name"=>"ZIMBABWE", "IsoCode"=>"ZW", "HasTown"=>"I"}]}

I want to populate a drop down list with this data. 我想用此数据填充下拉列表。 Code I am using to create the drop down box is: 我用来创建下拉框的代码是:

def country_selection_input options = {}
options.reverse_merge!(
  :attribute => :country_iso,
  :collection => transaction_form.get_countries,
  :input_html => {},
  :prompt => 'please select',
  :label => 'To Where'
)

This gives me a drop down box with a please select prompt and a list that consists of only the one word: Countries. 这给了我一个带请选择提示的下拉框,以及一个仅包含一个词的列表:国家。

The data is there - but I am not sure how to get it into the drop down list - and am sure I am missing something simple. 数据在那里-但是我不确定如何将其放入下拉列表-并且我肯定缺少简单的东西。 I have tried 我努力了

:label_method => :Name,

but get an error message of 但收到一条错误消息

undefined method `Name' for #<Array:0x007fc385cecbb0>

This will probably turn into a menu as I want to take action based on the country selected - but - this is the first step - getting the list to work. 当我想根据所选国家/地区采取行动时,这可能会变成一个菜单-但这是第一步-使列表生效。

The answer ended up being 答案最终是

def country_selection_input options = {}
countries = transaction_form.get_countries()[:Countries]
options.reverse_merge!(
  :attribute => :country_iso,
  :collection => countries,
  :label_method => :Name,
  :value_method => :IsoCode,
  :input_html => {},
  :prompt => 'please select',
  :label => 'To Where'
)

call_input_from_args_hash options

end 结束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails-从模型下拉列表 - ruby on rails - drop down list from model 从Rails中的ruby的选定下拉列表框中检索ID - retrieving the ID from a selected drop down list box in ruby on rails Ruby on Rails - 根据下拉选择填充数据库记录 - Ruby on Rails - Populate database record based on drop down selection Ruby Rails-NoMethodError,下拉列表 - Ruby Rails - NoMethodError , drop-down list 如何使用带有来自表的数据的列表填充下拉表? - How do I populate a drop down table using a list with data from a table? 使用数据填充下拉列表,并在Rails的查询中使用所选项目 - Populate drop down list with data and use selected item in query in Rails 从结果集中选择下拉列表值,以用作Ruby in Rails中的过滤器 - Drop down List of Values, selected from result set to use as filter in Ruby in Rails 如何将从下拉列表中选择的值传递给Rails模型上的ruby - how to pass the value selected from a drop down list to a ruby on rails model 从一个表中的下拉列表中获取值,并将其保存到另一个表中,以便在Rails中的Ruby中进行多对多关联 - fetching values in drop down list from one table and saving it to another table for many to many association in ruby on rails 如何从下拉列表中将所选值传递给Ruby中的控制器? - How can I pass the selected value from a drop down list to the controller in Ruby?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM