- 首页
- 前端开发
- Rails 4:如何通过 AJAX 基于另一个 collection_select 更新 collection_select?
Rails 4:如何通过 AJAX 基于另一个 collection_select 更新 collection_select?
[英] Rails 4: How to update a collection_select based on another collection_select through AJAX?
本文介绍了Rails 4:如何通过 AJAX 基于另一个 collection_select 更新 collection_select?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题:我需要根据对组织集合的选择来过滤单位集合.
选择组织后,单位下拉菜单应刷新以仅显示属于知情组织<的单位/强>.
我已经检查了这些问题:
还有这些文件:
这是我目前的代码:
模型
班级组织
观看次数app/views/projects/_form.html.erb
<%= form_for(@project) do |f|%><div class="field"><%= f.label :name %><br><%= f.text_field :name %>
<div class="field"><%= f.label :description %><br><%= f.text_area :描述%>
<div class="field"><%= f.label :organization_id %><br><%= f.collection_select :organization_id, Organization.all, :id, :name %>
<div class="field"><%= f.label :unit_id %><br><%= f.collection_select :unit_id, Unit.all.where(organization_id: :organization_id), :id, :name %>
<div class="actions"><%= f.submit %>
<%结束%>
控制器项目控制器
def new@project = Project.new结尾def project_paramsparams.require(:project).permit(:name,:description,:organization_id,:unit_id)结尾
我怎样才能做到这一点?
解决方案
我做到了,解决方案非常简单,但由于缺乏关于这个简单问题的更新材料,这使它变得比它应该有的麻烦:
config/routes.rb
get 'filter_units_by_organization' =>'项目#filter_units_by_organization'
controllers/projects_controller.rb
def filter_units_by_organization@filtered_units = Unit.where(organization_id: params[:selected_organization])结尾
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",类型:获取",数据:{ selected_organization: $("select#project_organization_id").val() }});});});
views/projects/_form.html.erb
<%= f.label :name %><br><%= f.text_field :name %>
<div class="field"><%= f.label :description %><br><%= f.text_area :描述%>
<div class="field"><%= f.label :organization_id %><br><%= f.collection_select :organization_id, Organization.all, :id, :name, { prompt: '请选择' } %>
<div class="field"><%= f.label :unit_id %><br><%= f.collection_select :unit_id, Unit.all.where(organization_id: :organization_id), :id, :name %>
<div class="actions"><%= f.submit %>
The problem:
I need to filter a collection of Units based upon the selection of a collection of Organizations.
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:
And these documentations:
This is my code so far:
Models
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
Views
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 %>
Controllers
Projects Controller
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屋!
查看全文