vendredi 17 juin 2016

Rails multiple dividers in a Ruby on Rails form Selector_tag

I have a selector within a standard ruby on rails form. The selector is for countries and the dividers with be continents. But I don't want to hard code the values of the countries in. I am returning several lists of countries (one for each continent) from the controller. And I want to put that in there instead of the hard coded array.

What I have:

HTML

  <%  grouped_options = [['North America',['USA','Mexico']],['Europe',['Denmark','Germany','France']]] %>

  <%= f.select :country_id, grouped_options_for_select(grouped_options), {:include_blank => "Please select a country"}, {:required => true, :class => "white_background"} %>

what I want:

   <%  grouped_options = [['North America',@north_america_names],['Europe',@europe_names]] %>

Edit: This option is not working and I am not sure why?

Controller

   @north_america_names = Country.select(:name, :id).where("continent = 'North America').distinct
   @europe_names = Country.select(:name, :id).where("continent = 'Europe').distinct

The controller works great, it returns a list of names and ids.

Aucun commentaire:

Enregistrer un commentaire