Topic: Selection of countries and states/regions

idk where to post this so I apologize if I posted in the wrong place.hehe

I am new in RoR web development so I really want to ask for your opinions on how I can implement this:

I have drop-down selection of countries and below that I have a selection of states or regions in the selected country.

please help me. thanks! smile

Re: Selection of countries and states/regions

I suggest having a look at this: http://railscasts.com/episodes/88-dynamic-select-menus
The video shows an example of exactly how to do that. I implemented that on an application im working on it works nicely. I used jQuery as supposed to prototype though.

Re: Selection of countries and states/regions

hi agm_ultimatex,

thanks for the link. I'll definitely watch it.

Re: Selection of countries and states/regions

hey. hi.

I followed the tutorial above unfortunately I can't make it work so I look for some other ways to implement the country selection.

this is what I did:

in the personnels/new.html.erb I added:

      <tr>
        <td align="right">Country:</td>
        <td><%= select_tag :country, options_for_select(@countries), { :id => "country_id"}%></td>
      </tr>
      <%= observe_field("country_id",
        :update => "loc-box",
        :on => "changed",
        :url => find_state_url,
        :with => "country_id") %>
      <%= observe_field("country_id",
        :update => "loc-box2",
        :on => "changed",
        :url => {:controller => 'personnels', :action => 'display_none'}) %>
      <%= observe_field("country_id",
        :update => "loc-box3",
        :on => "changed",
        :url => {:controller => 'personnels', :action => 'display_none'}) %>

      <tr id="loc-box"></tr>
      <tr id="loc-box2"></tr>
      <tr id="loc-box3"></tr>

and I have 4 partials,

in the _state_box.html.erb I have:

<td align="right">Region:</td>
<td><%= select_tag :state_or_region, options_for_select(@region), { :id => "region_id"}%></td>
<%= observe_field("region_id",
  :update => "loc-box2",
  :on => "changed",
  :url => {:controller => 'personnels', :action => 'find_province'},
  :with => "region_id") %>
<%= observe_field("region_id",
  :update => "loc-box3",
  :on => "changed",
  :url => {:controller => 'personnels', :action => 'display_none'}) %>

in _province_box.html.erb I have:

<td align="right">Province:</td>
<td><%= select_tag :province, options_for_select(@province), {:id => "province_id"} %></td>
<%= observe_field("province_id",
  :update => "loc-box3",
  :on => "changed",
  :url => {:controller => 'personnels', :action => 'find_city'},
  :with => "province_id") %>

in _city.html.erb I have:

<td align="right">City:</td>
<td><%= select_tag :city, options_for_select(@city)%></td>

I did not put any commands in _null.html.erb

in personnels controller I added:

  def find_state
    unless params[:country_id].empty?
      @country = Location.find(params[:country_id])
   
      @region = @country.sublocations.map{|region| [region.name.titleize, region.id]}.insert(0, ["Select a region",nil])
     
      if @country.name == "Philippines" then render :partial => 'state_box' else render :partial => 'null' end
    else
      render :partial => 'null'
    end
   
  end
 
  def find_province
    unless params[:region_id].empty?
      @region = Location.find(params[:region_id])
      @province = @region.sublocations.map{|province| [province.name.titleize, province.id]}.insert(0, ["Select a province",nil])

      render :partial => 'province_box'
    else
      render :partial => 'null'
    end
  end

  def find_city
    unless params[:province_id].empty?
      @province = Location.find(params[:province_id])
      @city = @province.sublocations.map{|province| [province.name.titleize, province.id]}.insert(0, ["Select a city",nil])

      if @city == [["Select a city", nil]] then render :partial => 'null' else render :partial => 'city_box' end
    else
      render :partial => 'null'
    end
  end

  def display_none
    render :partial => 'null'
  end

my problem is I have too many observe fields, and I feel like I'm not implementing the DRY principle, so if anyone has an idea on how I can optimize my codes, maybe help me put all javascripts in one javascript file and so I can just call it. hehe. thanks so much everyone for reading my post.