Commit 777c305a by Nguyen Quoc Kien

Fix bugs: Phan trang, show errors, SQL Injection, review code...

parent bd0447a0
class Admin::ProductsController < ApplicationController class Admin::ProductsController < ApplicationController
before_action :find_product, only: [:destroy, :edit,:update] before_action :find_product, only: [:destroy, :edit,:update]
before_action :get_categories, only: [:edit,:update, :new,:create]
before_action :authenticate_admin! before_action :authenticate_admin!
before_action :check_page, only: [:index]
def index def index
@products = Product.paginate(page: params[:page]).per_page(50) @products = Product.paginate(page: params[:page]).per_page(50)
...@@ -18,11 +20,6 @@ class Admin::ProductsController < ApplicationController ...@@ -18,11 +20,6 @@ class Admin::ProductsController < ApplicationController
def new def new
@product = Product.new @product = Product.new
@categories = Category.all
end
def edit
@categories = Category.all
end end
def create def create
...@@ -31,8 +28,7 @@ class Admin::ProductsController < ApplicationController ...@@ -31,8 +28,7 @@ class Admin::ProductsController < ApplicationController
flash[:success] = "Create product : Success" flash[:success] = "Create product : Success"
redirect_to admin_products_path redirect_to admin_products_path
else else
flash[:danger] = "Error: New product" render :new
redirect_to new_admin_product_path
end end
end end
...@@ -41,8 +37,7 @@ class Admin::ProductsController < ApplicationController ...@@ -41,8 +37,7 @@ class Admin::ProductsController < ApplicationController
flash[:success] = "Update product : Success" flash[:success] = "Update product : Success"
redirect_to admin_products_path redirect_to admin_products_path
else else
flash[:danger] = "Error: Update product" render :edit
redirect_to edit_admin_product_path(id: params[:id])
end end
end end
...@@ -55,4 +50,8 @@ class Admin::ProductsController < ApplicationController ...@@ -55,4 +50,8 @@ class Admin::ProductsController < ApplicationController
def product_params def product_params
params.require(:product).permit(:category_id, :name, :price, :image, :description) params.require(:product).permit(:category_id, :name, :price, :image, :description)
end end
def get_categories
@categories = Category.all
end
end end
class Admin::UsersController < ApplicationController class Admin::UsersController < ApplicationController
before_action :authenticate_admin! before_action :authenticate_admin!
before_action :check_page, only: [:index]
def index def index
@users = User.paginate(page: params[:page]).per_page(21) @users = User.paginate(page: params[:page]).per_page(21)
......
...@@ -28,4 +28,17 @@ class ApplicationController < ActionController::Base ...@@ -28,4 +28,17 @@ class ApplicationController < ActionController::Base
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) } devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password) } devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password) }
end end
def check_page
if (params[:page].to_i <= 0)
params[:page] = 1
end
if is_number?(params[:page]) == false
params[:page] = 1
end
end
def is_number? string
true if Float(string) rescue false
end
end end
class CartProductsController < ApplicationController class CartProductsController < ApplicationController
before_action :set_cart, only: [:create] before_action :set_cart, only: [:create, :update]
before_action :check_quantity?, only: [:create] before_action :check_quantity?, only: [:create]
def create def create
...@@ -9,11 +9,22 @@ class CartProductsController < ApplicationController ...@@ -9,11 +9,22 @@ class CartProductsController < ApplicationController
redirect_to cart_path(id: @user_id) redirect_to cart_path(id: @user_id)
flash[:success] = 'Products add to cart' flash[:success] = 'Products add to cart'
else else
redirect_to cart_path(id: @user_id) redirect_to products_path
flash[:success] = 'Errors: Quantity' flash[:danger] = 'Errors: Quantity'
end end
end end
def update
product = Product.find(params[:product_id])
if check_quantity?
update_product_to_cart(product.id.to_i, params[:quantity].to_i )
redirect_to cart_path(id: @user_id)
flash[:success] = 'Update successful'
else
redirect_to cart_path(id: @user_id)
flash[:danger] = 'Errors: Quantity'
end
end
def destroy def destroy
session[params[:id]].delete(params[:product_id]) session[params[:id]].delete(params[:product_id])
...@@ -25,15 +36,24 @@ class CartProductsController < ApplicationController ...@@ -25,15 +36,24 @@ class CartProductsController < ApplicationController
def add_product_to_cart(product_id, number) def add_product_to_cart(product_id, number)
number ||= 1 number ||= 1
i = 0 i = 0
@session[@user_id].each do |key, value| session[@user_id].each do |key, value|
if (key == product_id.to_s) if (key == product_id.to_s)
@session[@user_id][key] = number +value session[@user_id][key] = number +value
i = 1 i = 1
break break
end end
end end
if (i == 0) if (i == 0)
@session[@user_id][product_id] = number session[@user_id][product_id] = number
end
end
def update_product_to_cart(product_id, number)
session[@user_id].each do |key, value|
if (key == product_id.to_s)
session[@user_id][key] = number
break
end
end end
end end
......
class CartsController < ApplicationController class CartsController < ApplicationController
def new def new
@cart = Cart.new @cart = Cart.new
end end
...@@ -24,8 +23,7 @@ class CartsController < ApplicationController ...@@ -24,8 +23,7 @@ class CartsController < ApplicationController
flash[:success] = "Email to send" flash[:success] = "Email to send"
redirect_to products_path redirect_to products_path
else else
flash[:danger] = "Error: Create carts" render :new
redirect_to :back
end end
end end
......
class CategoriesController < ApplicationController class CategoriesController < ApplicationController
before_action :find_category, only: [:show]
before_action :check_page, only: [:show]
def index def index
@categories = Category.all @categories = Category.all
end end
def show def show
@categories = Category.all @categories = Category.all
@category = Category.find(params[:id])
@current_category = @category.id @current_category = @category.id
@products = @category.products.paginate(page: params[:page]).per_page(15) @products = @category.products.paginate(page: params[:page]).per_page(15)
end end
private
def find_category
if params[:id].to_i > (Category.count + 1)
redirect_to error_path
else
@category = Category.find(params[:id])
end
end
end end
class ProductsController < ApplicationController class ProductsController < ApplicationController
before_action :find_product, only: [:show] before_action :find_product, only: [:show]
before_action :check_page, only: [:index]
def index def index
@products = Product.paginate(page: params[:page]).per_page(21) @products = Product.paginate(page: params[:page]).per_page(21)
...@@ -13,7 +14,11 @@ class ProductsController < ApplicationController ...@@ -13,7 +14,11 @@ class ProductsController < ApplicationController
private private
def find_product def find_product
@product = Product.find(params[:id]) if params[:id].to_i > (Product.count + 1)
redirect_to error_path
else
@product = Product.find(params[:id])
end
end end
end end
class SearchController < ApplicationController class SearchController < ApplicationController
before_action :check_page, only: [:search]
def search def search
if params[:keyword].nil? if params[:keyword].nil?
......
class ShoppingHistoryController < ApplicationController class ShoppingHistoryController < ApplicationController
before_action :authenticate_user!
def index def index
@user = User.find(current_user.id) @user = User.find(current_user.id)
@carts_to_user = Cart.where(user_id: @user.id) @carts_to_user = Cart.where(user_id: @user.id)
......
...@@ -18,7 +18,7 @@ class Product < ActiveRecord::Base ...@@ -18,7 +18,7 @@ class Product < ActiveRecord::Base
def self.search(keyword) def self.search(keyword)
Product.where("name like '%?%'", keyword) Product.where("name like ?", "%#{keyword}%" )
end end
private private
......
...@@ -45,14 +45,12 @@ ...@@ -45,14 +45,12 @@
</tr> </tr>
<tr> <tr>
<td>Status: </td> <td>Status: </td>
<td colspan="3"><%= cart.status %></td> <td colspan="2"><%= cart.status %></td>
</tr> <td><% if cart.status != "Finish" %>
<tr> <%= button_to 'Next',admin_cart_path(id: cart.id, user_id: "buyers"), method: :put , class: "btn btn-primary" %>
<% if cart.status != "Finish" %>
<td colspan="4" style="text-align:right"><%= button_to 'Next',admin_cart_path(id: cart.id, user_id: "buyers"), method: :put , class: "btn btn-primary" %></td>
<% else %> <% else %>
<td colspan="4" style="text-align:right"><%= link_to 'Finished',"#" , class: "btn btn-danger" %></td> <%= link_to 'Finished',"#" , class: "btn btn-danger" %>
<% end %> <% end %></td>
</tr> </tr>
<% end %> <% end %>
<tr> <tr>
......
...@@ -45,14 +45,14 @@ ...@@ -45,14 +45,14 @@
</tr> </tr>
<tr> <tr>
<td>Status: </td> <td>Status: </td>
<td colspan="3"><%= cart_to_user.status %></td> <td colspan="2"><%= cart_to_user.status %></td>
</tr> <td>
<tr> <% if cart_to_user.status != "Finish" %>
<% if cart_to_user.status != "Finish" %> <%= button_to 'Next',admin_cart_path(id: cart_to_user.id, user_id: @user.id), method: :put , class: "btn btn-primary" %>
<td colspan="4" style="text-align:right"><%= button_to 'Next',admin_cart_path(id: cart_to_user.id, user_id: @user.id), method: :put , class: "btn btn-primary" %></td> <% else %>
<% else %> <%= link_to 'Finished',"#" , class: "btn btn-danger" %>
<td colspan="4" style="text-align:right"><%= link_to 'Finished',"#" , class: "btn btn-danger" %></td> <% end %>
<% end %> </td>
</tr> </tr>
<% end %> <% end %>
<tr> <tr>
......
<% provide(:title, "Order") %> <% provide(:title, "Order") %>
<div class="row"> <div class="row">
<% if @cart.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(@cart.errors.count, "error") %>.
</div>
<ul>
<% @cart.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="col-md-6 col-md-offset-3"> <div class="col-md-6 col-md-offset-3">
<h1>Đăng ký thông tin nhận hàng</h1> <h1>Đăng ký thông tin nhận hàng</h1>
<%= form_for @cart do |f| %> <%= form_for @cart do |f| %>
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
<th>Product name</th> <th>Product name</th>
<th>Price</th> <th>Price</th>
<th>Quantity</th> <th>Quantity</th>
<th>Update</th>
<th>Total price</th> <th>Total price</th>
<th>Delete</th> <th>Delete</th>
</tr> </tr>
...@@ -17,7 +18,11 @@ ...@@ -17,7 +18,11 @@
<tr> <tr>
<td><%= Product.find(key).name %> <td><%= Product.find(key).name %>
<td><%= number_to_currency(Product.find(key).price/100.00) %></td> <td><%= number_to_currency(Product.find(key).price/100.00) %></td>
<td><%= value %></td> <%= form_tag cart_product_path, method: :PATCH do %>
<%= hidden_field_tag :product_id, key %>
<td><%= number_field_tag :quantity, "#{value}", class: 'form-control', :style => "width: 70px"%></td>
<td><%= submit_tag "Update", :class => "btn btn-primary", :style => "width: 70px" %></td>
<% end %>
<td><%= number_to_currency(Product.find(key).price/100.000 * value.to_f) %></td> <td><%= number_to_currency(Product.find(key).price/100.000 * value.to_f) %></td>
<% total += Product.find(key).price/100.000 * value.to_f %> <% total += Product.find(key).price/100.000 * value.to_f %>
<td><%= link_to 'Delete', cart_product_path(product_id: key, id: params[:id]), data: { confirm: 'Are you sure?' }, method: :delete, <td><%= link_to 'Delete', cart_product_path(product_id: key, id: params[:id]), data: { confirm: 'Are you sure?' }, method: :delete,
...@@ -25,14 +30,11 @@ ...@@ -25,14 +30,11 @@
</tr> </tr>
<% end %> <% end %>
<tr> <tr>
<td>Total</td> <td colspan="4">Total</td>
<td></td>
<td></td>
<td> <%= number_to_currency(total) %></td> <td> <%= number_to_currency(total) %></td>
</tr> </tr>
<tr> <tr>
<td><%= link_to 'Back', products_path, class: "btn btn-danger" %></td> <td colspan="3"><%= link_to 'Back', products_path, class: "btn btn-danger" %></td>
<td> </td>
<td><%= button_to 'Empty cart', cart_path(id: params[:id]), method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-danger" %></td> <td><%= button_to 'Empty cart', cart_path(id: params[:id]), method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-danger" %></td>
<td><%= link_to 'Checkout', new_cart_path, class: "btn btn-danger" %></td> <td><%= link_to 'Checkout', new_cart_path, class: "btn btn-danger" %></td>
<td> </td> <td> </td>
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
<%= f.label :login %><br /> <%= f.label :login %><br />
<%= f.text_field :login, autofocus: true, class: 'form-control' %> <%= f.text_field :login, autofocus: true, class: 'form-control' %>
</div> </div>
<div class="field"> <div class="field">
<%= f.label :password %><br /> <%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off", class: 'form-control' %> <%= f.password_field :password, autocomplete: "off", class: 'form-control' %>
...@@ -17,13 +16,9 @@ ...@@ -17,13 +16,9 @@
<% end %> <% end %>
<% end -%> <% end -%>
</div> </div>
<div class="actions"> <div class="actions">
<%= f.submit "Log in", class: "btn btn-primary" %> <%= f.submit "Log in", class: "btn btn-primary" %>
</div> </div>
<% end %> <% end %>
<%= render "devise/shared/links" %> <%= render "devise/shared/links" %>
</div> </div>
\ No newline at end of file
<h1>Pages not found!</h1>
\ No newline at end of file
...@@ -9,11 +9,12 @@ Rails.application.routes.draw do ...@@ -9,11 +9,12 @@ Rails.application.routes.draw do
get 'help' => 'static_pages#help' get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about' get 'about' => 'static_pages#about'
get 'error' => 'static_pages#error'
resources :categories, only: [:index, :show] resources :categories, only: [:index, :show]
resources :products resources :products
resources :carts resources :carts
resources :cart_products, only: [:create, :destroy] resources :cart_products, only: [:create, :destroy, :update]
resources :shopping_history, only: [:index] resources :shopping_history, only: [:index]
namespace :admin do namespace :admin do
......
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