2012-09-24 37 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