Rails 4:如何通过AJAX基于另一个collection_select更新一个collection_select? [英] Rails 4: How to update a collection_select based on another collection_select through AJAX?

查看:74
本文介绍了Rails 4:如何通过AJAX基于另一个collection_select更新一个collection_select?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The problem: I need to filter a collection of Units based upon the selection of a collection of Organizations.

在选择组织后,单位下拉菜单应该刷新以仅显示属于已知的组织单位/strong>.

Upon selecting an Organization, the Unit dropdown-menu should refresh to show only the Units that belong to informed Organization.

我已经检查了以下问题:

I've checked these questions:

带有collection_select的铁轨中的ajax

如何添加onchange事件以选择Rails中的标签

轨道形式:更新使用AJAX基于其他collection_select值的collection_select选项

Rails form_for collection_select忽略了select_tag接受的远程ajax调用

远程选择,控制器需要更多的表单数据

以及这些文档:

  • 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

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

This is my code so far:

模型

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 %>

控制器 项目负责人

def new
  @project = Project.new
end

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

我该如何进行这项工作?

How can I make this work?

推荐答案

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

I made it, the solution was pretty simple, but the lack of updated material regarding this simple issue made it a chore bigger than it should have:

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

views/projects/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>

这篇关于Rails 4:如何通过AJAX基于另一个collection_select更新一个collection_select?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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