내가 Formtastic
과 직면 한 문제는 새로운 Order
을 작성하는 양식이 있다는 것입니다. 이 양식에서는 목록에서 여러 개의 기존 Food
항목을 선택하려고합니다. 이것들은 제가 제출하는 새로운 명령에 추가되어야합니다. 동시에 FoodOrder
조인 모델에서 속성을 설정하려고합니다. 이 모델은 내 양식에 필드를 갖고 싶어하는 정수형 속성을 가지고 있습니다.레일 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 Model을위한 필드를 얻는 방법을 모릅니다.
<%= 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
답변 해 주셔서 감사합니다. Form은 지금 내가 좋아하는 것처럼 멋지게 보입니다. 불행히도'고정 된 해시를 수정할 수 없습니다. '레일즈 런타임 오류가 발생합니다. 'FoodOrder'에서 수량을 수정할 수 없습니까? 그 어디에서 오류가오고 있는지 알 수 있습니까? – patrickdet
흠, 아마 그 방법의 파괴는 하루 중 나머지 시간 동안 내 전화를 사용하게 될 것이지만 대신에 "mark_for_destruction"을 시도해보십시오. 그러나 그것이 효과가 없다면 파괴를 완전히 제거하고 어떤 일이 일어나는지보십시오. – Azolo
빠른 답장을 보내 주셔서 감사합니다. '.mark_for_destruction'도 주석도 쓰지 않는다. 둘 다''SQLite3 :: ConstraintException : 제약 조건 실패 : INSERT INTO "food_orders"' – patrickdet