Take the 2-minute tour ×

I have two models: countries and users


class Country < ActiveRecord::Base
  has_many :users


class User < ActiveRecord::Base
  belongs_to :country

All users (from all countries) shown on users/index.html.erb page.


  def index
    @users = User.all


<%= collection_select(:user, :country_id, Country.all, :id, :country_name) %>
<%= render @users %>

There is also select menu with all countries on users/index.html.erb.

How can I do the following: when someone select specific country there will be shown users only from selected country?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

This can only be achieved using javascript.

The typical way to do it is with AJAX. Bind an onchange event to your select that requests a list of all users for the selected country. On the server you can then format that query as a set of select options, and insert then insert the response into the second drop down on the client.

This looks like a good tutorial: http://www.falsepositives.com/index.php/2010/05/28/building-a-casscading-drop-down-selection-list-for-ruby-on-rails-with-jquery-ajax/

Although you should try to find a more RESTful way of doing it.

share|improve this answer

Your Answer


