I’ve come back to rails after ignoring it for a few years and seem to have forgotten some things!
I have three models:
customer
product
order
When submitting an order, there are two fields to select the customer and the product. However, the logs (and db) show that the customer (customer.email) and product (product.title) is not submitted (just the IDs).
I’m fairly sure it’s something with the associations, but can’t work it out.
Order.rb
class Order < ApplicationRecord
belongs_to :customer
belongs_to :product
accepts_nested_attributes_for :customer
accepts_nested_attributes_for :product
validates :customer, presence: true
validates :product, presence: true
validates :quantity, presence: true
def total_price
product.price * quantity
end
end
product.rb
class Product < ApplicationRecord
has_many :orders, dependent: :destroy
end
customer.rb
class Customer < ApplicationRecord
has_many :orders, dependent: :destroy
end
orders_controller
class OrdersController < ApplicationController
before_action :set_order, only: %i[ show edit update destroy ]
# GET /orders or /orders.json
def index
@orders = Order.includes(:product, :customer).all
end
# GET /orders/1 or /orders/1.json
def show
@order = Order.includes(:product, :customer).find(params[:id])
end
# GET /orders/new
def new
@order = Order.new
end
# GET /orders/1/edit
def edit
end
# POST /orders or /orders.json
def create
@order = Order.new(order_params)
respond_to do |format|
if @order.save
format.html { redirect_to order_url(@order), notice: "Order was successfully created." }
format.json { render :show, status: :created, location: @order }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /orders/1 or /orders/1.json
def update
respond_to do |format|
if @order.update(order_params)
format.html { redirect_to order_url(@order), notice: "Order was successfully updated." }
format.json { render :show, status: :ok, location: @order }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end
# DELETE /orders/1 or /orders/1.json
def destroy
@order.destroy!
respond_to do |format|
format.html { redirect_to orders_url, notice: "Order was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_order
@order = Order.find(params[:id])
end
# Only allow a list of trusted parameters through.
def order_params
params.require(:order).permit(:product, :customer, :quantity, :product_id, :customer_id, :price, customer_attributes: [:id, :email], produc_attributes: [:id, :title])
end
end
_form
<%= form_with(model: order) do |form| %>
<% if order.errors.any? %>
<div style="color: red">
<h2><%= pluralize(order.errors.count, "error") %> prohibited this order from being saved:</h2>
<ul>
<% order.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form.label :product, style: "display: block" %>
<%= form.collection_select :product_id, Product.all, :id, :title, prompt: "Select Product" %>
</div>
<div>
<%= form.label :customer, style: "display: block" %>
<%= form.collection_select :customer_id, Customer.all, :id, :email, prompt: "Select Customer" %>
</div>
<div>
<%= form.label :quantity, style: "display: block" %>
<%= form.number_field :quantity %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>
``` (