2

내가 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 

답변

0

기본적으로 내가 만 0

위의 값을 가지고있는 사람을 저장 각 Food에 대한 FoodOrder 개체를 만드는 일을 끝낼 것이 무엇 이러한 특성 에 대해 reject_if에서 그렇게 할 수는 있지만 누군가가 3에서 0으로 변경 될 수있는 이전 FoodOrder을 삭제하지 않았으므로 대신 t 그는 quantity=FoodOrder에 대한 방법입니다.

이전에 formtastic을 사용 해본 적이 없지만 추측 해보고 작동해야한다고 말합니다.

<%= semantic_form_for [@restaurant, @order] do |f| %> 
    <%= f.inputs do %> 
    <%= f.input :comment %><br /> 
    <%= f.input :table_id %><br /> 
    <% @foods.each do |food| 
     <%= f.semantic_fields_for :food_orders, 
          @order.food_orders.detect_or_build_by_food(food) 
          do |food_order| %> 
     <%= food %> 
     <%= food_order.inputs :quantity %> 
     <% end %> 
    <% end %> 
    <% end %> 
    <%= f.buttons do %> 
    <%= f.commit_button %> 
    <% end %> 
<% end %> 

order.rb :

class Order < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :restaurant 
    has_many :foods, :through => :food_orders 
    has_many :food_orders do 
    def detect_or_build_by_food(food) 
     record = self.detect{ |food_order| food_order.food_id == food.id } 
     if record.nil? 
     record = self.build(:food => food) 
     end 
     record 
    end 
    end 

    accepts_nested_attributes_for :food_orders 
end 

food_order.rb :

class FoodOrder < ActiveRecord::Base 
    belongs_to :food 
    belongs_to :order 

    def quantity=(quantity) 
    if quantity <= 0 
     self.destroy 
    else 
     self[:quantity] = quantity 
    end 
    end 
end 

나는 희망이 작동하지만이 검증되지 않은 코드, 그래서 그래 ... 행운입니다!

+0

답변 해 주셔서 감사합니다. Form은 지금 내가 좋아하는 것처럼 멋지게 보입니다. 불행히도'고정 된 해시를 수정할 수 없습니다. '레일즈 런타임 오류가 발생합니다. 'FoodOrder'에서 수량을 수정할 수 없습니까? 그 어디에서 오류가오고 있는지 알 수 있습니까? – patrickdet

+0

흠, 아마 그 방법의 파괴는 하루 중 나머지 시간 동안 내 전화를 사용하게 될 것이지만 대신에 "mark_for_destruction"을 시도해보십시오. 그러나 그것이 효과가 없다면 파괴를 완전히 제거하고 어떤 일이 일어나는지보십시오. – Azolo

+0

빠른 답장을 보내 주셔서 감사합니다. '.mark_for_destruction'도 주석도 쓰지 않는다. 둘 다''SQLite3 :: ConstraintException : 제약 조건 실패 : INSERT INTO "food_orders"' – patrickdet