2013-06-27 39 views
0

創建嵌套的資源我有3款車型分別連接如下:從複選框

class Appointment < ActiveRecord::Base 
    has_many :service_items 
    has_many :services, through: :service_items 

    accepts_nested_attributes_for :service_items 
end 

class ServiceItem < ActiveRecord::Base 
    belongs_to :appointment 
    belongs_to :service 
end 

class Service < ActiveRecord::Base 
    has_many :service_items 
end 

對於新的任命,形成我想每個可用服務檢查的列表。並且應該從所選框中創建新約會的服務項目。

我該如何構建這樣的表單?

回答

1

我認爲你正在尋找的方法是collection_check_boxes。請在documentation中查看。

+0

我正在使用rails 3.2。我認爲它只能從4.0開始。任何建議3.2? – stevecross

+0

哦,你是對的,我忘了他是全新的東西,對不起。在3.2中,你可能會遍歷你的'Service'模型併爲每個模型生成一個複選框,並將檢查模型的ID存儲在一個參數中。 Google上的快速搜索返回了這篇文章,該文章演示了您試圖實現的目標:http://millarian.com/programming/ruby-on-rails/quick-tip-has_many-through-checkboxes/ – mario