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