0

我有一個遺留下來的Rails 3.2.14應用程序,我試圖通過關聯,grouped_collection_select和一些CoffeeScript來按區域約束設施列表。我原來的問題在這裏:CoffeeScript to dynamically change form field on change and load。藉助一些幫助,我們能夠最初解決問題並實施以下行爲。咖啡腳本來選擇動態表單域在Firefox中不工作

打電話創建區域被選中並且設施受到它們所屬的區域的限制,該字段被稱爲transfer_from_id。當使用未在Facility模型中列出的地址創建呼叫時,我們使用transfer_from_other字段輸入地址。發生這種情況時,transfer_from_id字段將故意留空,並注入NIL以防止CoffeeScript自動填充模型中的第一個工具。

編輯transfer_from_id包含值的調用時,transfer_from_id將以表單中的設施名稱顯示。

最初下面的代碼在Chrome中完美工作,但是當我去測試Firefox中的調用行爲時,它會自動填充列表中的第一個設施的transfer_from_id字段,即使我們預先在CoffeeScript的。編輯呼叫時,觀察到相同的行爲。

我想弄清楚在Firefox中如何解決這個問題,因爲我們使用這個應用程序跨平臺/瀏覽器。我真的很想讓這個功能正常工作,但我爲什麼不能在Firefox中工作而感到茫然。我用螢火蟲試圖調試,但我沒有看到控制檯中的任何錯誤。

下面是我的代碼庫摘錄這可能有助於說明我想要做的事:

來電/ _form.html.erb摘錄

<%= f.label :region %> 
    <%= f.collection_select(:region_id, Region.all, :id, :area, {:include_blank => true}, {:class => 'select', required: true}) %> 
    <%= f.label :caller_name %> 
    <%= f.text_field :caller_name, :placeholder => 'Jane Smith', required: true %> 
    <%= f.label :caller_phone %> 
    <%= f.text_field :caller_phone, :placeholder => 'xxx-xxx-xxxx', required: true %> 
    <%= f.label :Transfer_From %> 
    <%= f.grouped_collection_select :transfer_from_id, Region.order(:area), :active_facilities, :area, :id, :facility_name, {include_blank: true}, class: 'select' %> 
    <%= f.label :Transfer_From_Other%> 
    <%= f.text_field :transfer_from_other %> 
    <%= f.label :Location_Of_Patient %> 
    <%= f.text_field :facility_from_location %> 
    <%= f.label :Transfer_To %> 
    <%= f.grouped_collection_select :transfer_to_id, Region.order(:area), :active_facilities, :area, :id, :facility_name, {include_blank: true}, class: 'select' %> 
    <%= f.label :Transfer_To_Other %> 
    <%= f.text_field :transfer_to_other %> 

calls.js.coffee

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

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

    if options 
     # Set the options 
     $('#call_transfer_from_id').html(options) 
     # Add in a blank option at the top 
     $('#call_transfer_from_id').prepend("<option value=''></option>") 
    else 
     $('#call_transfer_from_id').empty()  

    $('#call_region_id').change -> 
    update_facilities() 

    update_facilities() 

facility.rb

belongs_to :region 
scope :active, where(status: "Active").order('facility_name ASC') 

region.rb

has_many :facilities 

    def active_facilities 
    self.facilities.active 
    end 

我很新的CoffeeScript和我JS/jQuery的技能不是很尖銳所以我現在可以使用一些這方面的幫助,得到它的工作跨瀏覽器。任何提示或建議非常感謝。我希望這不是一個重複的問題,如果我的問題不清楚或需要更多信息,請隨時告訴我。

回答

0

經過一些黑客攻擊和試用/錯誤之後,我能夠將此編輯提交到適用於所有瀏覽器的CoffeeScript中。看起來prepend方法在最新版本的Firefox中無法正常工作,所以我包含了一個空白選項,然後在一行中添加了工具選項數組。

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

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

    if options 
     # Set the options and include a blank option at the top 
     $('#call_transfer_from_id').html("<option value=''></option>" + options) 
     # Ensure that the blank option is selected 
     $('#call_transfer_from_id').attr("selected", "selected") 
    else 
     $('#call_transfer_from_id').empty() 

    $('#call_region_id').change -> 
    update_facilities() 

    update_facilities()