如何在订单中添加食品ID?

时间:2016-03-11 08:21:26

标签: ruby-on-rails

目前我正在通过rails制作一个简单的餐馆应用程序。现在,当我点击按钮顺序(与食物项目在同一行)时,它将引导我创建新的订单页面,但事情是订单不属于任何食物因为我不知道如何将食品ID整合到订单的形式中。我是否需要创建另一个类似order_item的模型来完成任务?因为以后,我想添加食物的数量,以及用户同时订购多种食物的能力(如购物车)。顺便问一下,在这种情况下我可以使用任何宝石吗?我看了狂欢宝石,但我不知道宝石是否适合我的应用? 以下是所有文件:

Menu Page

菜单/ drink.html.erb

<ul class="nav nav-tabs">
  <li class="nav-item">
    <%= link_to "Breakfast", menu_breakfast_path, class: "nav-link" %>
  </li>
  <li class="nav-item">
    <%= link_to "Lunch", menu_lunch_path, class: "nav-link" %>
  </li>
  <li class="nav-item">
    <%= link_to "Dinner", menu_dinner_path, class: "nav-link" %>
  </li>
  <li class="nav-item">
    <%= link_to "Drinks", menu_drink_path, class: "nav-link active" %>
  </li>
</ul>
<table class="table table-inverse sortable" >
  <thead>
    <tr>
      <th><%= sortable "Name" %></th>
      <th>Description</th>
      <th><%= sortable "Price" %></th>
      <th>Order</th>
    </tr>
  </thead>
  <% @foods.each do |food| %>
    <tbody>
      <tr>
        <td><%= link_to food.name, food %></td>
        <td><%= food.description %></td>
        <td>$<%= food.price %></td>
        <td><%= link_to "Order", new_order_path, class: "btn btn-primary" %></td>
      </tr>
    </tbody>
  <% end %>
</table>

顺序/ _form.htm.erb

<%= form_for(@order) do |f| %>
  <% if @order.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@order.errors.count, "error") %> prohibited this order from being saved:</h2>

      <ul>
      <% @order.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :coupon %><br>
    <%= f.text_field :coupon %>
  </div>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :username %>
  </div>
  <div class="field">
    <%= f.label :phone %><br>
    <%= f.telephone_field :phone %>
  </div>
  <div class="field">
    <%= f.label :address %><br>
    <%= f.text_field :address %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

orders_controller.rb:

def new
    @order = Order.new
  end
def create
    @order = Order.new(order_params)

    respond_to do |format|
      if @order.save
        format.html { redirect_to order_path, notice: 'Order was successfully created.' }
        format.json { render :show, status: :created, location: @order }
      else
        format.html { render :new }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end

order.rb

class Order < ActiveRecord::Base
  has_many :foods
end

food.rb

class Food < ActiveRecord::Base
  belongs_to :section
  belongs_to :order
end

1 个答案:

答案 0 :(得分:0)

作为第一步,我会对你的模特和协会做出一些修正:

class Order < ActiveRecord::Base
  belongs_to :customer
  has_many :order_items, dependent: :destroy
  has_many :products, through: :order_items
class OrderItem < ActiveRecord::Base
  belongs_to :order
  belongs_to :product
class Product < ActiveRecord::Base
  has_many :order_items
  has_many :orders, through: :order_items

您可以根据自己的应用交换模型名称。

您可能会看到nested_form gem的价值,并探索使用inverse_of以及这些关联,这些关联可与accepts_nested_attributes一起使用,以创造更好的体验