Javascript 在Rails 5中使用ajax更新复选框选项

Javascript 在Rails 5中使用ajax更新复选框选项,javascript,ruby-on-rails,ruby,ajax,forms,Javascript,Ruby On Rails,Ruby,Ajax,Forms,对应用程序的一点解释。这是一个多校区组织的旅游注册系统。有人应该能够进来,从选择框中选择一个校园,并让选项复选框和巡演日期单选按钮更新为校园特定的 当有人更改选择框时,我会触发脚本,但当我转到路径时,它会不断返回404,url看起来非常不稳定。另外,我知道当我试图用新选项替换.html部分时,我会遇到麻烦。好的,不用麻烦了,下面是代码。任何指导都将不胜感激 _form.html.erb 注册\u controller.rb 更新校园选项和日期.js.erb ... <div class="

对应用程序的一点解释。这是一个多校区组织的旅游注册系统。有人应该能够进来,从选择框中选择一个校园,并让选项复选框和巡演日期单选按钮更新为校园特定的

当有人更改选择框时,我会触发脚本,但当我转到路径时,它会不断返回404,url看起来非常不稳定。另外,我知道当我试图用新选项替换.html部分时,我会遇到麻烦。好的,不用麻烦了,下面是代码。任何指导都将不胜感激

_form.html.erb 注册\u controller.rb 更新校园选项和日期.js.erb
...
<div class="field">
    <%= label_tag 'Campus' %>
    <%= select_tag 'campuses', options_from_collection_for_select(@churches, 'id', 'name', selected: (@registration.event.church.id unless @registration.event.nil?) )%>
</div>

<div class="field">
    <%= label_tag 'Areas of Interest' %>
    <div id="options_container">
        <%= f.collection_check_boxes :option_ids, @options, :id, :ministry_area do |b| %>
        <div class="collection-check-box">
            <%= b.check_box %>
            <%= b.label %>
        </div>
        <% end %>
    </div>
</div>

<div class="field">
    <%= label_tag 'Tour Dates' %>
    <div id="events_container">
        <%= f.collection_radio_buttons(:event_id, @events, :id, :event_datetime) do |b| %>
        <div class="radio">
            <%= b.radio_button + " " + b.label %>
        </div>
        <% end %>
    </div>
</div>

<div class="actions">
    <%= f.submit params[:action] == 'edit' ? "Update Registration" : "Register", class: 'button' %>
</div>
<% end %>

<script>
    $(document).ready(function () {
        $('#campuses').change(function () {
            $.ajax({
                url: "<%= update_campus_options_and_events_path %>",
                data: {
                    campus_id: $('#campuses').val()
                },
                dataType: "script"
            });
        });
    });
</script>
Rails.application.routes.draw do
  resources :registrations
  root 'registrations#index'
  get 'update_campus_options_and_events', to: 'registrations#update_campus_options_and_events'
end
...
  def update_campus_options_and_events
    @events = Event.find(params[:campus_id])
    @options = Option.find(params[:campus_id])
    respond_to do |format|
      format.html
      format.js {}
    end
  end
...
console.log('Options changed via ajax');
$('#options_container').html("<%= escape_javascript(<%= f.collection_check_boxes :option_ids, @options, :id, :ministry_area do |b| %>) %>");
$('#events_container').html("<%= escape_javascript(<%= f.collection_radio_buttons(:event_id, @events, :id, :event_datetime) do |b| %>) %>");