Reputation: 295
I have two models: countries and users
country.rb
class Country < ActiveRecord::Base
has_many :users
end
user.rb
class User < ActiveRecord::Base
belongs_to :country
end
All users (from all countries) shown on users/index.html.erb page.
users_controller.rb
def index
@users = User.all
end
users/index.html.erb
<%= 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?
Upvotes: 0
Views: 198
Reputation: 1133
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.
Upvotes: 1