Commit 8cfa4dc8 by Tan Phat Nguyen

Change products to admin products management. product show item

parent 99deb8e6
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
// Place all the styles related to the admin/products controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
class Admin::ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
def index
@products = Product.all
end
def new
@product = Product.new
end
def create
@product = Product.new(product_params)
if @product.save
redirect_to [:admin, @product]
else
render :new
end
end
def show
end
def edit
end
def update
if @product.update_attributes(product_params)
redirect_to [:admin, @product]
else
render :edit
end
end
def destroy
@product.destroy
redirect_to admin_products_path
end
private
def product_params
params.require(:product).permit(:name, :price, :description, :category_id, :photo, :remote_photo_url)
end
def set_product
@product = Product.find(params[:id])
end
end
class CategoryController < ApplicationController
class CategoriesController < ApplicationController
def show
@category = Category.find(params[:id])
@products = @category.products.page(params[:page])
......
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
def index
@products = Product.all
end
def new
@product = Product.new
end
def create
@product = Product.new(product_params)
if @product.save
redirect_to @product
else
render 'new'
end
end
def show
end
def edit
end
def update
if @product.update_attributes(product_params)
redirect_to @product
else
render 'edit'
end
end
def destroy
@product.destroy
redirect_to products_path
end
def detail
@product = Product.find_by(id: params[:id])
end
private
def product_params
params.require(:product).permit(:name, :price, :description, :category_id, :photo, :remote_photo_url)
end
def set_product
@product = Product.find(params[:id])
end
end
module CategoriesHelper
end
class Category < ActiveRecord::Base
has_many :products, dependent: :destroy
def recent_products
products.limit(4)
def newest_products
products.newest
end
end
class Product < ActiveRecord::Base
default_scope { order(:name) }
belongs_to :category
......@@ -9,6 +8,8 @@ class Product < ActiveRecord::Base
validates :description, presence: true
validate :photo_size
scope :newest, -> { order(created_at: :desc).limit(4) }
mount_uploader :photo, ImageUploader
paginates_per 8
......
<%= form_for @product, html: {multipart: true} do |f| %>
<%= form_for [:admin, @product], html: { multipart: true } do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="form-group">
<%= image_tag @product.photo.url, width: '140', height: '140', class: 'img-rounded' %>
......@@ -30,5 +30,5 @@
<%= f.text_field :remote_photo_url %>
</div>
<%= f.submit class: "btn btn-primary" %>
<%= link_to 'Cancel', products_path, class: "btn btn-primary" %>
<%= link_to 'Cancel', admin_products_path, class: "btn btn-primary" %>
<% end %>
\ No newline at end of file
<tr>
<td><%= product.name %></td>
<td><%= product.price %></td>
<td><%= product.description %></td>
<td><%= product.category.name %></td>
<td><%= image_tag product.photo_url.to_s, width: '140', height: '140', class: 'img-rounded' %></td>
<td><%= link_to 'Show', [:admin, product] %></td>
<td><%= link_to 'Edit', edit_admin_product_path(product) %></td>
<td><%= link_to 'Destroy', [:admin, product], method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
......@@ -13,4 +13,4 @@
<%= render @products %>
</table>
</div>
<%= link_to 'New Product', new_product_path, class: "btn btn-primary" %>
<%= link_to 'New Product', new_admin_product_path, class: "btn btn-primary" %>
\ No newline at end of file
<% provide(:title, @product.name) %>
<h1 class='page-header'>Show Product</h1>
<p>Name: <b><%= @product.name%></b></p>
<p>Price: <b>$<%= @product.price%></b></p>
<p>Description: <b><%= @product.description%></b></p>
<p>Category: <b><%= @product.category.name%></b></p>
<%= image_tag @product.photo.url, width: '140', height: '140', class: 'img-rounded' %> <br/><br>
<%= link_to 'Back', admin_products_path, class: 'btn btn-primary' %>
<%= link_to 'Edit', edit_admin_product_path(@product), class: 'btn btn-primary' %>
\ No newline at end of file
<%= provide(:title, 'Home') %>
<h1 class='page-header'>VenShop Apps</h1>
<% @categories.each do |category| %>
<% if category.products.any? %>
<% if category.products.any? %>
<h2 class='sub-header'><%= category.name %></h2>
<div class='row placeholders'>
<% category.recent_products.each do |product| %>
<div class='col-xs-6 col-sm-3 placeholder'>
<div style='height: 234px;'>
<%= image_tag product.photo_url.to_s, width: '140', height: '140', class: 'img-thumbnail' %>
</div>
<h4><%= link_to product.name, product_detail_path(product) %></h4>
<span class='price-col'>$<%= product.price %></span>
</div>
<% end %>
<%= render category.newest_products %>
</div>
<div>
<%= link_to '>>View more...', category_path(category) %>
</div>
<% end %>
<% end %>
\ No newline at end of file
<%= provide(:title, "Category #{@category.name if @category}") %>
<h1 class='page-header'>Category <%= @category.name if @category %></h1>
<div class='row placeholders'>
<%= render @products %>
</div>
<%= paginate @products %>
\ No newline at end of file
<%= provide(:title, "Category #{@category.name if @category}") %>
<h1 class='page-header'>Category <%= @category.name if @category %></h1>
<div class='row placeholders'>
<% @products.each do |product| %>
<div class='col-xs-6 col-sm-3 placeholder'>
<div style='height: 234px;'>
<%= image_tag product.photo_url.to_s, width: '140', height: '140', class: 'img-thumbnail' %>
</div>
<h4><%= link_to product.name, product_detail_path(product) %></h4>
<span class='price-col'>$<%= product.price %></span>
</div>
<% end %>
</div>
<%= paginate @products %>
\ No newline at end of file
<%= provide(:title, 'Home') %>
<h1 class='page-header'>VenShop Apps</h1>
<% @categories.each do |category| %>
<% if category.products.any? %>
<h2 class='sub-header'><%= category.name %></h2>
<div class='row placeholders'>
<% category.recent_products.each do |product| %>
<div class='col-xs-6 col-sm-3 placeholder'>
<div style='height: 234px;'>
<%= image_tag product.photo_url.to_s, width: '140', height: '140', class: 'img-thumbnail' %>
</div>
<h4><%= link_to product.name, product_detail_path(product) %></h4>
<span class='price-col'>$<%= product.price %></span>
</div>
<% end %>
</div>
<div>
<%= link_to '>>View more...', category_path(category) %>
</div>
<% end %>
<% end %>
\ No newline at end of file
<%= render @categories %>
\ No newline at end of file
<tr>
<td><%= product.name %></td>
<td><%= product.price %></td>
<td><%= product.description %></td>
<td><%= product.category.name %></td>
<td><%= image_tag product.photo_url.to_s, width: '140', height: '140', class: 'img-rounded' %></td>
<td><%= link_to 'Show', product_path(product) %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product_path(product), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<div class='col-xs-6 col-sm-3 placeholder'>
<div style='height: 234px;'>
<%= image_tag product.photo_url.to_s, width: '140', height: '140', class: 'img-thumbnail' %>
</div>
<h4><%= link_to product.name, product_path(product) %></h4>
<span class='price-col'>$<%= product.price %></span>
</div>
\ No newline at end of file
<% provide(:title, "Item detail") %>
<% if @product %>
<h1 class='page-header'><%= @product.name %></h1>
<div class='page-header row'>
<div class='col-xs-4'>
<%= image_tag @product.photo_url.to_s, width: '349', height: '500', class: 'img-rounded' %>
</div>
<div class='col-xs-8'>
Item: <h4><%= @product.name %></h4>
Price: <p class='price-col'>$<%= @product.price %></p>
Date: <p><%= @product.created_at.strftime('%d-%m-%Y') %></p>
</div>
</div>
<div class='row'>
<b>Description:</b>
<p></p>
<p><%= @product.description %></p>
</div>
<div class='row'>
<%= link_to 'Buy', '#', class: 'btn btn-primary' %>
</div>
<% end %>
\ No newline at end of file
<% provide(:title, @product.name) %>
<h1 class='page-header'>Show Product</h1>
<p>Name: <b><%= @product.name%></b></p>
<p>Price: <b>$<%= @product.price%></b></p>
<p>Description: <b><%= @product.description%></b></p>
<p>Category: <b><%= @product.category.name%></b></p>
<%= image_tag @product.photo.url, width: '140', height: '140', class: 'img-rounded' %> <br/><br>
<%= link_to 'Back', products_path, class: 'btn btn-primary' %>
<%= link_to 'Edit', edit_product_path(@product), class: 'btn btn-primary' %>
\ No newline at end of file
<% provide(:title, "Item detail") %>
<% if @product %>
<h1 class='page-header'><%= @product.name %></h1>
<div class='page-header row'>
<div class='col-xs-4'>
<%= image_tag @product.photo_url.to_s, width: '349', height: '500', class: 'img-rounded' %>
</div>
<div class='col-xs-8'>
Item: <h4><%= @product.name %></h4>
Price: <p class='price-col'>$<%= @product.price %></p>
Date: <p><%= @product.created_at.strftime('%d-%m-%Y') %></p>
</div>
</div>
<div class='row'>
<b>Description:</b>
<p></p>
<p><%= @product.description %></p>
</div>
<div class='row'>
<%= link_to 'Buy', '#', class: 'btn btn-primary' %>
</div>
<% end %>
\ No newline at end of file
Rails.application.routes.draw do
namespace :admin do
resources :products
end
get 'amazon/welcome'
get 'amazon/index'
get '/category/:id', to: 'category#show', as: 'category'
get '/category/:id', to: 'categories#show', as: 'category'
get '/product/detail/:id', to: 'products#detail', as: 'product_detail'
resources :products
resources :products, only: [:show]
get 'home' => 'home#index'
root 'home#index'
......
require 'test_helper'
class Admin::ProductsControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
end
test "should get show" do
get :show
assert_response :success
end
test "should get new" do
get :new
assert_response :success
end
test "should get create" do
get :create
assert_response :success
end
test "should get edit" do
get :edit
assert_response :success
end
test "should get update" do
get :update
assert_response :success
end
test "should get destroy" do
get :destroy
assert_response :success
end
end
require 'test_helper'
class CategoryControllerTest < ActionController::TestCase
class CategoriesControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
......
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