1
votes

I'm working on a project with Elixir and Phoenix, something like POS system. I have several food items that I keep in my db

schema "foods" do
field :name, :string
field :price, :integer
belongs_to :category, Pos1.Category
has_many :order_items, Pos1.OrderItem

as well as orders (for keeping the orders)

schema "orders" do
field :number_of_customers, :integer
belongs_to :table, Pos1.Table
has_many :order_items, Pos1.OrderItem

and order items (for keeping the order items linking to the specific order_id)

schema "order_items" do
field :quantity, :integer
belongs_to :order, Pos1.Order
belongs_to :food, Pos1.Food

I want to make a page, that will list all the food from the db. Each of those food should have a button, which onclick adds that food to the order_item table.

This is what i came up with so far (show.html):

<%= for food <- @foods do %>
<%= food.name %>
<%= render Pos1.OrderItemView, "form.html", changeset: @order_item_changeset, foods: @foods, action: order_order_item_path(@conn, :create, @order) %>
<% end %>

and the form(form.html)

<%= form_for @changeset, @action, fn f -> %>
<div class="form-group">
<%= label f, :food_id, class: "control-label" %>
<%= select f, :food_id, foods_to_select(@foods), class: "form-control" %>
<%= error_tag f, :food_id %>
</div>

<div class="form-group">
<%= label f, :quantity, class: "control-label" %>
<%= number_input f, :quantity, class: "form-control" %>
<%= error_tag f, :quantity %>
</div>

<div class="form-group">
<%= submit "Addfood", class: "btn btn-primary" %>
</div>
<% end %>

However, I am struggling with the food_id. In the code above I still have to select the food and quantity in the changeset form like here ,but is it possible to have food_id automatically assigned(to be inserted into db), based on what particular food i am on the show.html page? So, all the foods will just have quantity selection.

For example, if I have two foods: coke and pepsi. On show.html I'll have two boxes: one for coke, one for pepsi. And each box will have the quantity input, and "submit button". So if i enter 1 in coke box and onclick, it will be inserted into the order_items table. And vise versa for pepsi, or any other foods.

Thanks in advance!

1

1 Answers

3
votes

You can pass the food_id to the form, and then in form.html.eex, put @food_id in a hidden input tag. You also don't need to pass @foods to form.html.eex then.

show.html.eex:

<%= for food <- @foods do %>
  <%= food.name %>
  <%= render Pos1.OrderItemView, "form.html", changeset: @order_item_changeset, action: order_order_item_path(@conn, :create, @order), food_id: food.id %>
<% end %>

form.html.eex:

<%= form_for @changeset, @action, fn f -> %>
  <%= hidden_input f, :food_id, value: @food_id %>

  <div class="form-group">
    <%= label f, :quantity, class: "control-label" %>
    <%= number_input f, :quantity, class: "form-control" %>
    <%= error_tag f, :quantity %>
  </div>

  <div class="form-group">
    <%= submit "Add food", class: "btn btn-primary" %>
  </div>
<% end %>