Commit 754cff60 by Tan Phat Nguyen

create user, login, logout, setting

parent 6044ada2
......@@ -13,4 +13,5 @@
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap
//= require_tree .
......@@ -84,9 +84,10 @@ h2 {
.col-sm-3 {
width: 20%;
margin-bottom: 10px;
}
.button_to .btn-default {
.button_to .btn-default,a.btn-default,a.btn-default:hover{
background-color: #FFC741;
}
......@@ -96,10 +97,17 @@ h2 {
.nav-icon {
background-image: url(http://g-ecx.images-amazon.com/images/G/01/gno/sprites/global-sprite-32-v3._V325667795_.png);
//background-position: -10px 1024px;
background-position: -10px -266px;
}
.nav-icon:hover {
background-position: -10px 1024px;
}
.alert-alert {
@extend .alert-warning;
}
.alert-notice {
@extend .alert-success;
}
\ No newline at end of file
class Admin::ProductsController < ApplicationController
before_action :authenticate_user!
before_action :set_product, only: [:show, :edit, :update, :destroy]
def index
......
class CartsController < ApplicationController
before_action :set_cart, only: [:show, :edit, :update, :destroy]
def index
@carts = Cart.all
end
def show
@carts = current_cart
end
def new
@cart = Cart.new
end
def edit
end
def create
@cart = Cart.new(cart_params)
respond_to do |format|
if @cart.save
format.html { redirect_to @cart, notice: 'Cart was successfully created.' }
format.json { render :show, status: :created, location: @cart }
else
format.html { render :new }
format.json { render json: @cart.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @cart.update(cart_params)
format.html { redirect_to @cart, notice: 'Cart was successfully updated.' }
format.json { render :show, status: :ok, location: @cart }
else
format.html { render :edit }
format.json { render json: @cart.errors, status: :unprocessable_entity }
end
end
end
def destroy
@cart.destroy
current_cart.destroy
flash[:success] = 'Cart is empty.'
redirect_to root_path
end
private
def set_cart
@cart = Cart.find(params[:id])
end
def cart_params
params[:cart]
end
end
class LineItemsController < ApplicationController
before_action :set_line_item, only: [:show, :edit, :update, :destroy]
def index
@line_items = LineItem.all
end
def show
end
def new
@line_item = LineItem.new
end
def edit
end
def create
@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product.id)
@line_item = current_cart.add_product(product.id)
if @line_item.save
flash[:success] = 'You have just one item in cart.'
redirect_to @line_item.cart
end
end
def update
respond_to do |format|
if @line_item.update(line_item_params)
format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
format.json { render :show, status: :ok, location: @line_item }
else
format.html { render :edit }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
def destroy
@line_item.destroy
respond_to do |format|
format.html { redirect_to line_items_url, notice: 'Line item was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_line_item
@line_item = LineItem.find(params[:id])
end
def line_item_params
params.require(:line_item).permit(:product_id, :cart_id)
end
end
......@@ -2,7 +2,7 @@ class OrdersController < ApplicationController
before_action :set_order, only: [:show, :edit, :update, :destroy]
def index
@orders = Order.newest.page(params[:page])
@orders = Order.page(params[:page])
end
def show
......@@ -22,7 +22,7 @@ class OrdersController < ApplicationController
@order.add_line_items_from_cart(current_cart)
if @order.save
Cart.destroy(session[:cart_id])
current_cart.destroy
session[:cart_id] = nil
flash[:success] = 'Successfully. Thank you for your order.'
redirect_to root_path
......
......@@ -3,12 +3,14 @@ class Cart < ActiveRecord::Base
def add_product(product_id)
current_item = line_items.where(product_id: product_id).first
if current_item
current_item.quantity += 1
else
current_item = LineItem.new(product_id: product_id)
line_items << current_item
end
current_item
end
......
class Order < ActiveRecord::Base
scope :newest, -> { order(created_at: :desc) }
default_scope { order(created_at: :desc) }
PAYMENT_TYPES = [ 'Check', 'Credit card', 'Purchase order' ]
......
<nav class='navbar navbar-inverse navbar-fixed-top'>
<div class='container-fluid'>
<div class='navbar-header'>
<%= link_to 'VenShop Apps', root_path, class: 'navbar-brand' %>
</div>
<div id='navbar' class='navbar-collapse collapse'>
<ul class='nav navbar-nav navbar-right'>
<li><%= link_to 'Home', root_path %></li>
<% if user_signed_in? %>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Order History", '#' %></li>
<li><%= link_to "Settings", edit_user_registration_path %></li>
<li class="divider"></li>
<li><%= link_to "Log out", destroy_user_session_path, method: "delete" %></li>
</ul>
</li>
<% else %>
<li><%= link_to 'Log in', new_user_session_path %></li>
<li><%= link_to 'Register', new_user_registration_path %></li>
<% end %>
<li style='width: 36px'><%= link_to '', cart_path(current_cart), class: 'nav-icon' %></li>
</ul>
<form class='navbar-form navbar-right'>
<input type='text' class='form-control' placeholder='Search...'>
</form>
</div>
</div>
</nav>
\ No newline at end of file
<%= form_for(@cart) do |f| %>
<% if @cart.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@cart.errors.count, "error") %> prohibited this cart from being saved:</h2>
<ul>
<% @cart.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<h1>Editing Cart</h1>
<%= render 'form' %>
<%= link_to 'Show', @cart %> |
<%= link_to 'Back', carts_path %>
<p id="notice"><%= notice %></p>
<h1>Listing Carts</h1>
<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @carts.each do |cart| %>
<tr>
<td><%= link_to 'Show', cart %></td>
<td><%= link_to 'Edit', edit_cart_path(cart) %></td>
<td><%= link_to 'Destroy', cart, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Cart', new_cart_path %>
<h1>New Cart</h1>
<%= render 'form' %>
<%= link_to 'Back', carts_path %>
<%= provide(:title, 'Current Cart') %>
<h1 class='page-header'>Your Cart Infomation</h1>
<table class="table">
<%= render @cart.line_items %>
<%= render current_cart.line_items %>
<tr class="success">
<td colspan='3' class='r-text'>Total</td>
<td>$<%= @cart.total_price %></td>
<td>$<%= current_cart.total_price %></td>
</tr>
</table>
<div class='row'>
<div class='col-md-5'></div>
<div class='col-md-1'>
<%= button_to 'Checkout', new_order_url, method: :get, class: 'btn btn-default' %>
<%= link_to 'Checkout', new_order_path, class: 'btn btn-default' %>
</div>
<div class='col-md-1'>
<%= button_to 'Empty Cart', @cart, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-default' %>
<%= link_to 'Empty Cart', current_cart, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-default' %>
</div>
</div>
<li><%= link_to category.name, category_path(category) %></li>
\ No newline at end of file
<h2>Forgot your password?</h2>
<% provide(:title, 'Forgot password') %>
<h1 class='page-header'>Forgot your password?</h1>
<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }) do |f| %>
<%= devise_error_messages! %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<div class="form-group">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="actions">
<%= f.submit "Send me reset password instructions" %>
<%= f.email_field :email,class: 'form-control', placeholder: 'Please input email address', autofocus: true %>
</div>
<%= f.submit "Send me reset password instructions", class: "btn btn-primary" %>
<% end %>
<%= render "devise/shared/links" %>
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<% provide(:title, 'Edit user') %>
<h1 class='page-header'>Edit <%= resource_name.to_s.humanize %></h1>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control', placeholder: 'Please input email address', autofocus: true %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off" %>
<div class="form-group">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i>
<%= f.password_field :password, class: 'form-control', autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
<div class="form-group">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control', autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Update" %>
<div class="form-group">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i>
<%= f.password_field :current_password, class: 'form-control', autocomplete: "off" %>
</div>
<%= f.submit "Update", class: "btn btn-primary" %>
<% end %>
<h3>Cancel my account</h3>
<h2>Cancel my account</h2>
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete, class: 'btn btn-danger' %></p>
<%= link_to "Back", :back %>
<%= link_to "Back", :back, class: "btn btn-primary" %>
<h2>Sign up</h2>
<% provide(:title, 'Sign up') %>
<h1 class='page-header'>Sign up</h1>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control', placeholder: 'Please input email address', autofocus: true %>
</div>
<div class="field">
<div class="form-group">
<%= f.label :password %>
<% if @validatable %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
<% end %>
<%= f.password_field :password, class: 'form-control', placeholder: 'Please input secret password', autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
<div class="form-group">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control', placeholder: 'Please confirm secret password', autocomplete: "off" %>
</div>
<%= f.submit "Sign up", class: "btn btn-primary" %>
<% end %>
<%= render "devise/shared/links" %>
<h2>Log in</h2>
<% provide(:title, 'Log in') %>
<h1 class='page-header'>Log in</h1>
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control', placeholder: 'Please input email address', autofocus: true %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off" %>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control', placeholder: 'Please input secret password', autocomplete: "off" %>
</div>
<% if devise_mapping.rememberable? -%>
<div class="field">
<div class="form-group">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
<% end -%>
<div class="actions">
<%= f.submit "Log in" %>
</div>
<%= f.submit "Log in", class: "btn btn-primary" %>
<% end %>
<%= render "devise/shared/links" %>
......@@ -6,8 +6,22 @@
<div id='navbar' class='navbar-collapse collapse'>
<ul class='nav navbar-nav navbar-right'>
<li><%= link_to 'Home', root_path %></li>
<li><%= link_to 'Log in', '#' %></li>
<li><%= link_to 'Register', '#' %></li>
<% if user_signed_in? %>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Order History", '#' %></li>
<li><%= link_to "Settings", edit_user_registration_path %></li>
<li class="divider"></li>
<li><%= link_to "Log out", destroy_user_session_path, method: "delete" %></li>
</ul>
</li>
<% else %>
<li><%= link_to 'Log in', new_user_session_path %></li>
<li><%= link_to 'Register', new_user_registration_path %></li>
<% end %>
<li style='width: 36px'><%= link_to '', cart_path(current_cart), class: 'nav-icon' %></li>
</ul>
<form class='navbar-form navbar-right'>
......
......@@ -7,20 +7,18 @@
<%= csrf_meta_tags %>
</head>
<body>
<%= render 'layouts/header' %>
<%= render 'header' %>
<div class='container-fluid'>
<div class='row'>
<div class='col-sm-3 col-md-2 sidebar'>
<ul class='nav nav-sidebar'>
<li class='active'><a href='#'>Categories</a></li>
<% Category.all.each do |category| %>
<li><%= link_to category.name, category_path(category) %></li>
<% end %>
<%= render partial: 'categories/link', collection: Category.all, as: :category %>
</ul>
</div>
<div class='col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main'>
<%= render 'layouts/errors' %>
<%= render 'errors' %>
<%= yield %>
</div>
</div>
......@@ -28,5 +26,5 @@
</body>
<%= render 'layouts/footer' %>
<%= render 'footer' %>
</html>
<%= form_for(@line_item) do |f| %>
<% if @line_item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@line_item.errors.count, "error") %> prohibited this line_item from being saved:</h2>
<ul>
<% @line_item.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :product_id %><br>
<%= f.number_field :product_id %>
</div>
<div class="field">
<%= f.label :cart_id %><br>
<%= f.number_field :cart_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<h1>Editing Line Item</h1>
<%= render 'form' %>
<%= link_to 'Show', @line_item %> |
<%= link_to 'Back', line_items_path %>
<p id="notice"><%= notice %></p>
<h1>Listing Line Items</h1>
<table>
<thead>
<tr>
<th>Product</th>
<th>Cart</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @line_items.each do |line_item| %>
<tr>
<td><%= line_item.product_id %></td>
<td><%= line_item.cart_id %></td>
<td><%= link_to 'Show', line_item %></td>
<td><%= link_to 'Edit', edit_line_item_path(line_item) %></td>
<td><%= link_to 'Destroy', line_item, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Line item', new_line_item_path %>
<h1>New Line Item</h1>
<%= render 'form' %>
<%= link_to 'Back', line_items_path %>
<p id="notice"><%= notice %></p>
<p>
<strong>Product:</strong>
<%= @line_item.product_id %>
</p>
<p>
<strong>Cart:</strong>
<%= @line_item.cart_id %>
</p>
<%= link_to 'Edit', edit_line_item_path(@line_item) %> |
<%= link_to 'Back', line_items_path %>
Rails.application.routes.draw do
devise_for :users
resources :orders
resources :line_items, only: [ :create ]
resources :line_items, only: [:create]
resources :carts, only: [ :show, :destroy ]
resources :carts, only: [:show, :destroy]
namespace :admin do
resources :products
......@@ -16,7 +17,7 @@ Rails.application.routes.draw do
get '/category/:id', to: 'categories#show', as: 'category'
resources :products, only: [ :show ]
resources :products, only: [:show]
get 'home' => 'home#index'
root 'home#index'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment