Rails依赖于表单中的collection_select字段 [英] Rails dependent collection_select fields in form

查看:160
本文介绍了Rails依赖于表单中的collection_select字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Rails应用程序,但无法找到如何处理从属下拉列表。
我有3个模型:
- 有几个组的类别
- 有多个成员的组
- 成员

当选择一个类别时,我希望此类别中的组填充第二个下拉列表(组和成员之间的相同内容)。



我拥有下面的形式(显然这不是我想要的,因为我拿着特定模型的所有物品)...

 < 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>

使这些字段相关的最佳方法是什么?我在网络上发现了几个关于这个问题的主题,但无法真正找到答案。 解决方案

呼叫。
在刚开始的填充类别下拉菜单中。并在类别的变化使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]
#使用val查找记录
options = YourModel.collect {| x | '#{x.id}':'#{x.label}'}
render:text => {#{options.join(,)}}
end

现在你不需要部分。


I'm working on a rails app but cannot find how to handle dependent drop down lists. I have 3 models: - Category which has several groups - Group which has several members - Member

When a category is selected, I'd like the groups within this category to populate the second drop down list (and same thing between group and member).

I have the following form (obviously this is not working how I'd like as I take all the items for the particular 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 those fields dependent ? I found several topics regarding this on the web but could not really find an answer.

解决方案

The best way of doing it is AJAX call. In the starting just populate category drop-down. And on change of category make an ajax call and populate groups. And do same for the members on groups drop-down change.

Try something like this:

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

In your controller:

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

Now you dont need partial.

这篇关于Rails依赖于表单中的collection_select字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆