Created
October 20, 2016 10:29
-
-
Save samseen/ac66ef9a4d020eee2d9d95f87808b687 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Pragprog Books Online Store</title> | |
<%= stylesheet_link_tag "application", :media => "all" %> | |
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> | |
<%= csrf_meta_tags %> | |
</head> | |
<body class='<%= controller.controller_name %>'> | |
<div id="banner"> | |
<%= image_tag("logo.png") %> | |
<%= @page_title || "Pragmatic Bookshelf" %> | |
</div> | |
<div id="columns"> | |
<div id="side"> | |
<div id="cart"> | |
<% if @cart %> | |
<%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %> | |
<%= render @cart %> | |
<% end %> | |
<% end %> | |
</div> | |
<ul> | |
<li><a href="#">Home</a></li> | |
<li><a href="#/faq">Questions</a></li> | |
<li><a href="#/news">News</a></li> | |
<li><a href="#/contact">Contact</a></li> | |
</ul> | |
</div> | |
<div id="main"> | |
<%= yield %> | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Order < ActiveRecord::Base | |
has_many :line_items, dependent: :destroy | |
PAYMENT_TYPES = [ "Check", "Credit card", "Purchase order"] | |
validates :name, :address, :email, presence: true | |
validates :pay_type, inclusion: PAYMENT_TYPES | |
def add_line_items_from_cart(cart) | |
cart.line_items.each do |item| | |
item.cart_id = nil | |
line_items << item | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OrdersController < ApplicationController | |
before_action :set_order, only: [:show, :edit, :update, :destroy] | |
# GET /orders | |
# GET /orders.json | |
def index | |
#@orders = Order.paginate page: params[:page], order: 'created_at desc', per_page: 10 | |
@orders = Order.paginate page: params[:page], order: 'created_at desc', per_page: 10 | |
respond_to do |format| | |
format.html # index.html.erb | |
format.json { render json: @orders } | |
end | |
end | |
# GET /orders/1 | |
# GET /orders/1.json | |
def show | |
end | |
# GET /orders/new | |
def new | |
@cart = current_cart | |
if @cart.line_items.empty? | |
redirect_to store_url, notice: "Your cart is empty" | |
return | |
end | |
@order = Order.new | |
respond_to do |format| | |
format.html # new.html.erb | |
format.json { render json: @order } | |
end | |
end | |
# GET /orders/1/edit | |
def edit | |
end | |
# POST /orders | |
# POST /orders.json | |
def create | |
@order = Order.new(order_params[:order]) | |
@order.add_line_items_from_cart(current_cart) | |
respond_to do |format| | |
if @order.save | |
Cart.destroy(session[:cart_id]) | |
session[:cart_id] = nil | |
format.html { redirect_to store_url, notice: 'Thank you for your order.' } | |
format.html { redirect_to @order, notice: 'Order was successfully created.' } | |
format.json { render json: @order, status: :created, location: @order } | |
else | |
@cart = current_cart | |
format.html { render action: "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: 'Order was successfully updated.' } | |
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 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 | |
# Never trust parameters from the scary internet, only allow the white list through. | |
def order_params | |
params.require(:order).permit(:name, :address, :email, :pay_type) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment