我与Formtastic
面临的问题属性是,我有一个表格,以创建一个新的Order
。在这种形式下,我想从列表中选择多个现有的Food
项目。这些应该被添加到我提交的新订单中。同时我也想在FoodOrder
连接模型中设置属性。此模型具有整数数量属性,我希望在该表单中有一个字段。Rails的Formtastic嵌套形式 - 形式从加盟模式
什么我基本上找的是,列出了所有食品和在同一行,因为它属于食品项目把一个场为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
谢谢您的回答!表格现在看起来不错,就像我在找的那样。不幸的是,我得到'不能修改冻结哈希'Rails运行时错误。不允许修改'FoodOrder'中的数量吗?任何想法可能来自哪里? – patrickdet 2012-01-08 11:32:17
嗯,它可能是在我要成为我的电话了一天休息的方法摧毁,但你可以试试“mark_for_destruction”代替。但是,如果这样做不起作用,那就完全消灭这个破坏,看看会发生什么。 – Azolo 2012-01-08 11:50:40
感谢您的快速回复。 '.mark_for_destruction'也没有评论它的工作。都导致'SQLite3 :: ConstraintException:约束失败:INSERT INTO“food_orders”' – patrickdet 2012-01-08 12:05:32