【发布时间】:2012-01-07 12:01:31
【问题描述】:
Formtastic 面临的问题是我有一个表单来创建一个新的Order。在此表单中,我想从列表中选择多个现有的 Food 项目。这些应添加到我提交的新订单中。同时我也想在FoodOrderjoin模型中设置属性。这个模型有一个整数数量属性,我想在我的表单中有一个字段。
我基本上要寻找的是一个表格,它列出了所有食品,并将quantity 的字段与它所属的食品放在同一行。
模型
class Order < ActiveRecord::Base
belongs_to :user
belongs_to :restaurant
has_many :food_orders
has_many :foods, :through => :food_orders
end
class FoodOrder < ActiveRecord::Base
belongs_to :food
belongs_to :order
end
class Food < ActiveRecord::Base
has_many :food_orders
has_many :orders, :through => :food_orders
belongs_to :category
end
这是迄今为止我尝试过的表单版本之一。但我很困惑,不知道如何获取 FoodOrder 模型的字段。
<%= semantic_form_for [@restaurant, @order] do |f| %>
<%= f.inputs do %>
<%= f.input :comment %><br />
<%= f.input :table_id %><br />
<%# <%= f.input :foods, :as => :check_boxes %>
<%= f.inputs :for => :foods do |food| %>
<%= food %>
<%= food.inputs :quantity %>
<% end %>
<% end %>
<%= f.buttons do %>
<%= f.commit_button %>
<% end %>
<% end %>
我的模型有这些属性
create_table "food_orders", :force => true do |t|
t.integer "quantity", :null => false
t.decimal "price", :null => false
t.integer "food_id", :null => false
t.integer "order_id", :null => false
t.text "comment"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "foods", :force => true do |t|
t.integer "category_id", :null => false
t.string "name", :null => false
t.string "description"
t.string "image"
t.decimal "default_price", :null => false
t.boolean "active", :default => true, :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "orders", :force => true do |t|
t.integer "restaurant_id", :null => false
t.integer "user_id", :null => false
t.integer "table_id", :null => false
t.decimal "total", :null => false
t.datetime "finished_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
【问题讨论】:
标签: ruby-on-rails forms nested-forms formtastic nested-attributes