0

アプリに2つのモデルがあります。Shopping_listと製品

ショッピングリストには多くの商品を含めることができ、商品は多くのショッピングリストの一部にすることができます。

ショッピングリスト-

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 / editにアクセスした場合、ユーザーに表示してもらいたい-

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の使用中の進め方がわからない。

アドバイスをいただければ幸いです。

4

1 に答える 1

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 %>
于 2012-09-24T13:59:19.217 に答える