简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM