Rails dependent fields collect fields in the form

I am working on a rails application but cannot find how to handle dependent dropdowns. I have 3 models: - A category that has several groups - A group that has several members - Member

When a category is selected, I would like the groups in this category to populate the second drop-down list (and the same between the group and the member).

I have the following view (obviously, this does not work, as I would like, when I take all the elements for a specific model) ...

<div class="field">
<%= f.collection_select(:category, Category.find(:all), :id, :name, {:include_blank => 'Category'}) %>
</div>
<div class="field">
<%= f.collection_select(:group, Group.find(:all), :id, :name, {:include_blank => 'Group'}) %>
</div>
<div class="field">
<%= f.collection_select(:member, Member.find(:all), :id, :name, {:include_blank => 'Member'}) %>
</div>
<div class="actions">
<%= f.submit %>
</div>

What would be the best way to make these fields dependent? I found several topics regarding this on the Internet, but could not find an answer.

+5
source share
2 answers

- AJAX. . ajax- . .

- :

$("#category']").click(function(){
  var url = '/get_drop_down_options?category_id=' + $(this).val()
  $("#group").removeOption(/./)
  $.get(url, function(data) {
    $('#group').addOption(data, false);
  });
});

:

def get_drop_down_options
  val = params[:category_id]
  #Use val to find records
  options = YourModel.collect{|x| "'#{x.id}' : '#{x.label}'"}    
  render :text => "{#{options.join(",")}}" 
end

.

+4

Id , jquery ajax magic: , ajax , , , div .

0

All Articles