Rails的动态改变款式选择在形式上基于表单域 [英] Rails Dynamically Changing Collection Select In Form Based on form field

查看:198
本文介绍了Rails的动态改变款式选择在形式上基于表单域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails 3.2.14应用程序,跟踪电话,每次通话都有来自该基金的模型是一个协会,一个皮卡和空投设备

I have a Rails 3.2.14 app that tracks calls and each call has a pickup and dropoff facility from the Facility model which is an association.

在通话模式存在的区域模型(即休斯顿,达拉斯,奥斯汀,等等),我们选择基于这样的电话是来自一个区域。关联

In the call model there is an association for the Region model (i.e. Houston, Dallas, Austin, etc) where we select a region based on where the call is coming from.

我想要做的是能够选择一个特定的区域(即休斯顿)和皮卡工厂集合只显示休斯敦地区的设施。

What I'd like to do is be able to select a specific region (i.e. Houston) and in the pickup facility collection only show Houston region facilities.

我假设,开始了与我需要设置为这样的设施和区域之间的关系:

I'm assuming to start off with I'd need to setup a relationship between Facility and Region as such:

facility.rb

attr_accessible :region_id
belongs_to :region

region.rb

has_many :facilities

然后,我需要设置每个设施与REGION_ID的相应区域(如休斯顿,达拉斯,等等),这样的关联匹配正常工作。

Then I'd need to set each facility with a region_id that matches the respective region (i.e Houston, Dallas, etc) so the association works properly.

什么我不能确定的是如何选择特定区域和只有在形式显示区域的设施。我假设我会用一些jQuery / JS /阿贾克斯在这里做到这一点,但不知道如何使它发挥作用。

What I'm unsure of is how to select the specific region and only have the facilities in that region display in the form. I'm assuming I would use some jQuery/JS/Ajax here to make it happen but not sure how to make it work.

下面是一个什么样目前看起来像我的电话,设施和区域模型的摘录:

Here's an excerpt of what my call, facility, and region models currently look like:

call.rb

belongs_to :transferred_from, :foreign_key => :transfer_from_id, :class_name => 'Facility'
belongs_to :transferred_to, :foreign_key => :transfer_to_id, :class_name => 'Facility'
belongs_to :region

facility.rb

has_many :calls_transferred_from, :foreign_key => :transfer_from_id, :class_name => 'Call'
has_many :calls_transferred_to, :foreign_key => :transfer_to_id, :class_name => 'Call'

region.rb

has_many :calls

下面是什么我的电话的形式部分看起来像一个摘录:

Here's an excerpt of what my call form partial looks like:

_form.html.erb

<%= form_for(@call) do |f| %>
  <%= f.label :region %>
  <%= f.collection_select(:region_id, Region.all, :id, :area, {:include_blank => true}, {:class => 'select', required: true}) %>
  <%= f.label :Transfer_From %>
  <%= f.collection_select(:transfer_from_id, Facility.active.order("facility_name ASC"), :id, :facility_name, {:include_blank => true}, {:class => 'select'}) %>
 <%= f.button "Submit", class: 'btn btn-info btn-large', data: {disable_with: "<i class='icon-spinner'></i>Processing..."} %> 
<% end %>

如果任何这是混淆或需要澄清,请让我知道。总之,我想选择一个区域(休斯敦),并只显示在选择该区域是在形式休斯敦地区的设施。

If any of this is confusing or needs clarification, please let me know. To summarize, I'm trying to select a region (Houston) and only show the Facilities that are in the Houston region in the form when that region is selected.

在此先感谢您的帮助和提示,你可以提供。

Thanks in advance for any help or tips you can provide.

推荐答案

根据离他的Railscasts动态选择菜单,我想出了以下内容:

Based off of the Railscasts dynamic select menus I've come up with the following:

calls.js.coffee

jQuery ->
  facilities = $('#call_transfer_from_id').html()

  $('#call_region_id').change ->
    region = $('#call_region_id :selected').text()
    options = $(facilities).filter("optgroup[label=#{region}]").html()

    if options
      $('#call_transfer_from_id').html(options)   
    else
      $('#call_transfer_from_id').empty()

_form.html.erb

<%= f.grouped_collection_select :transfer_from_id, Region.order(:area), :facilities, :area, :id, :facility_name, {include_blank: true}, class: 'select' %>

上述grouped_collection_select的问题是,它的呼唤:设施,其中列出了所有的设施,我需要调用的Facility.active(范围我创建了基金只列出活动的设施)相当。我怎样才能把这种从内部grouped_collection_select?

The problem with the above grouped_collection_select is that it's calling :facilities which lists all the facilities, I need to call the equivalent of Facility.active (scope I created on Facility to only list active facilities). How can I call this from within grouped_collection_select?

到目前为止,在一个新的呼叫,我能够选择的区域,则该区域的设备显示只有在野外(transfer_from_id被调用)。当编辑的电话,试图改变设备我psented与设施因地区而不是只针对该区域的设施进行分组的完整列表$ P $。如果我要缩小下来我必须改变区域再改回原来的特定地区的设施完全展现出来。

So far in a new call I'm able to select the region then the facilities for that region show up only in the from field (transfer_from_id being called). When editing the call and trying to change the facility I'm presented with a full list of facilities grouped by region instead of just the facilities for that region. If I want to narrow it down I have to change region then change it back to the original for the specific region's facilities to show up solely.

我觉得我做这个进步,但它不工作了我想要的方式。我要答案,更有意义和更清晰的真正开放。

I think I'm making progress with this, but it's not working out the way I want it. I'm really open to answers that make more sense and are more clear.

这篇关于Rails的动态改变款式选择在形式上基于表单域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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