gpt4 book ai didi

ruby-on-rails - Rails 4 : How to update a collection_select based on another collection_select through AJAX?

转载 作者:行者123 更新时间:2023-12-03 15:22:32 27 4
gpt4 key购买 nike

问题:
我需要根据组织集合的选择来过滤单位集合

选择单位后,单位下拉菜单应刷新以仅显示属于已知单位单位

我检查了以下问题:

  • Rails forms: updating collection_select options based on other collection_select value using AJAX
  • UJS, AJAX, Rails 4, form_for collection_select to pass value into method and return value back to form
  • ajax in rails with collection_select
  • How to add an onchange event to select tag in rails
  • Rails forms: updating collection_select options based on other collection_select value using AJAX
  • Rails form_for collection_select ignoring remote ajax call that select_tag accepts
  • remote select, controller needs more form data

  • 这些文档:
  • http://guides.rubyonrails.org/form_helpers.html
  • http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
  • http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_from_collection_for_select

  • 到目前为止,这是我的代码:

    模型
    class Organization < ActiveRecord::Base
    has_many :units
    has_many :projects, through: :units
    end
    class Unit < ActiveRecord::Base
    belongs_to :organization
    has_many :projects
    end
    class Project < ActiveRecord::Base
    belongs_to :organizaton
    has_one :organization, through: :unit
    end

    浏览
    app/views/projects/_form.html.erb
    <%= form_for(@project) do |f| %>

    <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
    </div>
    <div class="field">
    <%= f.label :description %><br>
    <%= f.text_area :description %>
    </div>
    <div class="field">
    <%= f.label :organization_id %><br>
    <%= f.collection_select :organization_id, Organization.all, :id, :name %>
    </div>
    <div class="field">
    <%= f.label :unit_id %><br>
    <%= f.collection_select :unit_id, Unit.all.where(organization_id: :organization_id), :id, :name %>
    </div>
    <div class="actions">
    <%= f.submit %>
    </div>
    <% end %>

    Controller
    项目负责人
    def new
    @project = Project.new
    end

    def project_params
    params.require(:project).permit(:name, :description, :organization_id, :unit_id)
    end

    我该如何进行这项工作?

    最佳答案

    我做到了,解决方案非常简单,但是由于缺少有关此简单问题的更新资料,因此使它比应做的事情繁琐得多:

    config/routes.rb

     get 'filter_units_by_organization' => 'projects#filter_units_by_organization'

    controllers/projects_controller.rb
    def filter_units_by_organization
    @filtered_units = Unit.where(organization_id: params[:selected_organization])
    end

    View /项目/filter_units_by_organization.js.erb
    $('select#project_unit_id').html('<%= j options_from_collection_for_select(@filtered_units, :id, :name) %>');

    Assets /javascripts/application.js
    $(function() {
    $("select#project_organization_id").on("change", function() {
    $.ajax({
    url: "/filter_units_by_organization",
    type: "GET",
    data: { selected_organization: $("select#project_organization_id").val() }
    });
    });
    });

    views/projects/_form.html.erb
    <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
    </div>
    <div class="field">
    <%= f.label :description %><br>
    <%= f.text_area :description %>
    </div>
    <div class="field">
    <%= f.label :organization_id %><br>
    <%= f.collection_select :organization_id, Organization.all, :id, :name, { prompt: 'Please select' } %>
    </div>
    <div class="field">
    <%= f.label :unit_id %><br>
    <%= f.collection_select :unit_id, Unit.all.where(organization_id: :organization_id), :id, :name %>
    </div>
    <div class="actions">
    <%= f.submit %>
    </div>

    关于ruby-on-rails - Rails 4 : How to update a collection_select based on another collection_select through AJAX?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37230426/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com