2016-05-14 29 views
1

問題: 我需要過濾一組單元的集合,其基礎是選擇一組組織。Rails 4:如何通過AJAX更新基於另一個collection_select的collection_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 

查看 應用程序/視圖/項目/ _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 

我怎樣才能使這項工作?

回答

6

我做到了,該解決方案是非常簡單的,但由於缺乏這方面簡單的問題更新材料製成它更大一個苦差事比它應該有:

的config/routes.rb中

get 'filter_units_by_organization' => 'projects#filter_units_by_organization' 

控制器/ projects_controller.rb

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

層的意見/項目/ filter_units_by_organization.js.erb

$('select#project_unit_id').html('<%= j options_from_collection_for_select(@filtered_units, :id, :name) %>'); 

資產/ Java腳本/ 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() } 
     }); 
    }); 
}); 

的意見/項目/ _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> 
1

而不是在projects表上放置一個副本organization_id您應該設置關係經歷單位模型。

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 :unit 
    has_one :organizaton, through: :unit 
end 

這就避免了尷尬的問題,你必須以確保單位和項目有相同的organizaton_id

這也意味着您在創建項目時不再需要輸入來選擇組織 - 只是單位。

+1

如果您仍希望組織爲用戶選擇能夠縮小選項範圍需要實現它作爲ajax調用。但是,這本身就是整個教程,如果你是新手入門,那麼以後可能會更好。 – max

+0

感謝@max的提醒,我會用你的答案重構代碼:)關於ajax,我想這就是我的意圖,我會重新回答這個問題。我在rails上使用ajax做了一些基本的事情,比如livesearch過濾器,排序和分頁,還設置了視圖模板來響應模態,但是我對JS和Rails之間「對話」的瞭解非常有限,您知道任何我可以更多地瞭解它的地方?謝謝 –

+1

我不使用'js.erb'或內置'remote:true'選項。恕我直言,它只是導致服務器端和客戶端邏輯和糟糕的API的總clusterfuck。它是鐵軌中最糟糕的部分之一。我只使用普通的舊jquery ajax處理程序和來自控制器的JSON響應。 – max