関連、grouped_collection_select、およびいくつかのCoffeeScriptを介して地域ごとに施設のリストを制限しようとしているレガシーRails 3.2.14アプリがあります。私の元の質問はここにあります: CoffeeScript to dynamic form field on change and load . いくつかの助けを借りて、最初に問題を解決し、次の動作を実装することができました。
Call create では地域が選択され、施設は所属する地域によって制約されます。このフィールドは transfer_from_id と呼ばれます。ファシリティ モデルにリストされていないアドレスでコールを作成する場合、transfer_from_other フィールドを使用してアドレスを入力します。その場合、transfer_from_id フィールドは意図的に空白のままにし、NIL を挿入して、CoffeeScript がモデルの最初の施設を自動入力しないようにします。
transfer_from_id に値が含まれる呼び出しを編集すると、transfer_from_id がフォームに施設名とともに表示されます。
元々、以下のコードは Chrome で完全に機能していましたが、作成呼び出しで Firefox で動作をテストしたところ、CoffeeScript で空白のオプションを追加したにもかかわらず、リストの最初のファシリティが transfer_from_id フィールドに自動入力されました。通話を編集すると、同じ動作が観察されます。
このアプリをクロスプラットフォーム/ブラウザーで使用しているため、Firefox でこの問題を解決する方法を見つけたいと思います。私は本当にこの機能を動かしたいと思っていますが、なぜ Firefox で動かないのか途方に暮れています。これをデバッグするために firebug を使用しましたが、コンソールにエラーは表示されません。
以下は、私がやろうとしていることを説明するのに役立つかもしれない私のコードベースからの抜粋です:
calls/_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 %>
呼び出し.js.コーヒー
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()
施設.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 のスキルは今のところあまり鋭くないので、クロスブラウザーで動作させるためにこれに関する助けを借りることができます。ヒントやアドバイスは大歓迎です。これが重複した質問と見なされないことを願っています。私の問題が明確でない場合、またはさらに情報が必要な場合は、お気軽にお知らせください。