2012-09-24 40 views
0

我在我的應用程序中有2個模型。一個Shopping_list和一個商品許多到多種形式,mongoid和導軌3

購物清單可以包含很多產品,產品可以是許多購物清單的一部分。

購物清單 -

class Shopping_list 
    include Mongoid::Document 
    field :name, :type => String 
    has_and_belongs_to_many :products 
end 

而且產品

class Product 
    include Mongoid::Document 
    field :name, :type => String 
    has_and_belongs_to_many :shopping_lists 
end 

如果用戶訪問/和shopping_list/SOME_ID /編輯我想讓他們看到 -

a) The name of the product in a text box 
b) A series of checkboxes listing all of the products that they can check to add to that list. 
c) A submit button. 

我控制器看起來像這樣

def edit 
    @shopping_list = Shopping_list.find(params[:id]) 
    render :action => 'edit' 
end 

我的看法是這樣的

<%= simple_form_for(@shopping_list) do |f| %> 
    <%= f.input :name %> 
    <%= f.input :articles, :as => :check_boxes %> #I know this is completely wrong. What do I do to fix? 
    <%= f.button :submit %> 
<% end %> 

這根本不起作用,我有點難倒。不知道如何使用Mongoid時繼續。

建議感激。

回答

0

試試這個

<%= simple_form_for(@shopping_list) do |f| %> 
    <%= f.input :name %> 
    <%= f.association :products,:collection => @shopping_list.products.collect{ |p| [p.name, p.id] }, :as => :check_boxes %> 
    <%= f.button :submit %> 
<% end %> 
+0

感謝。這是我正在尋找的'協會'。 – Finnnn