ivanp
(Ivan David Pipitone)
1
before_action :set_order, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
# GET /orders
# GET /orders.json
def index
@orders = Order.all
end
# GET /orders/1
# GET /orders/1.json
def show
end
# GET /orders/new
def new
@order = Order.new
@listing = Listing.find(params[:listing_id])
end
# GET /orders/1/edit
def edit
end
# POST /orders
# POST /orders.json
def create
@order = Order.new(order_params)
@listing = Listing.find(params[:listing_id])
@seller = @listing.user
@order.listing_id = @listing.id
@order.buyer_id = current_user.id
@order.seller_id = @seller.id
respond_to do |format|
if @order.save
format.html { redirect_to root_url, notice: 'Pedido creado' }
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
# PATCH/PUT /orders/1
# PATCH/PUT /orders/1.json
def update
respond_to do |format|
if @order.update(order_params)
format.html { redirect_to @order, notice: 'El pedido fue actualizado' }
format.json { render :show, status: :ok, location: @order }
else
format.html { render :edit }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end
# DELETE /orders/1
# DELETE /orders/1.json
def destroy
@order.destroy
respond_to do |format|
format.html { redirect_to listing_orders_path, notice: 'El pedido fue eliminado con exito' }
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(:address, :city, :state)
end
end`
My Index Page:
<p id="notice"><%= notice %></p>
<h1>Pedidos</h1>
<table>
<thead>
<tr>
<th>Direccion</th>
<th>Ciudad</th>
<th>Provincia</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @orders.each do |order| %>
<tr>
<td><%= order.address %></td>
<td><%= order.city %></td>
<td><%= order.state %></td>
<td><%= link_to 'Mostrar', listing_order_path(order.listing, order) %></td>
<td><%= link_to 'Editar', edit_listing_order_path(order.listing, order) %></td>
<td><%= link_to 'Eliminar', listing_order_path(order.listing, order), method: :delete, data: { confirm: 'Estas seguro?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'Nuevo Pedido', new_listing_order_path %>
Edit Form:
<h1>Editing Order</h1>
<%= render 'form', order: @order.listing %>
<%= link_to 'Atras', listing_orders_path %>
Form:
<%= form_with(model: [ @listing, order ], local: true) do |form| %>
<% 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">
<%= form.label :address %>
<%= form.text_field :address %>
</div>
<div class="field">
<%= form.label :city %>
<%= form.text_field :city %>
</div>
<div class="field">
<%= form.label :state %>
<%= form.text_field :state %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>