Commit a01f17cd by Hoang Phuc Do

Fix merge request #2

parent 764def09
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
protect_from_forgery with: :exception protect_from_forgery with: :exception
# rescue_from ActiveRecord::RecordNotFound, :with => :render_404
def authenticate_active_admin_user! def authenticate_active_admin_user!
authenticate_user! authenticate_user!
...@@ -8,4 +9,12 @@ class ApplicationController < ActionController::Base ...@@ -8,4 +9,12 @@ class ApplicationController < ActionController::Base
redirect_to root_path redirect_to root_path
end end
end end
def render_404
respond_to do |format|
format.html { render file: "#{Rails.root}/public/404", layout: false, status: :not_found }
format.xml { head :not_found }
format.any { head :not_found }
end
end
end end
class ProductsController < ApplicationController class ProductsController < ApplicationController
before_action :authenticate_user!, only: [:new, :edit, :create, :update, :destroy] before_action :authenticate_user!, only: [:new, :edit, :create, :update, :destroy]
before_action :correct_user, only: [:edit, :destroy] before_action :set_product, only: [:show]
before_action :user_can_edit_product, only: [:edit, :update, :destroy]
# GET /products/new
def new def new
@product = current_user.products.build @product = Product.new
end end
# POST /products
def create def create
@product = current_user.products.build(product_params) @product = Product.new(product_params.merge(user_id: current_user.id))
if @product.save if @product.save
redirect_to root_url redirect_to root_url, flash: { success: "Product #{@product.title} is sucessfully created" }
else else
render 'new' render 'new'
end end
end end
def edit # PATCH/PUT /products/1
@product = Product.find(params[:id])
end
def show
@product = Product.find(params[:id])
end
def update def update
@product = Product.find(params[:id])
if @product.update(product_params) if @product.update(product_params)
redirect_to root_url redirect_to root_url, flash: { success: "Product #{@product.title} is sucessfully updated" }
else else
render 'edit' render 'edit'
end end
end end
# DELETE /products/1
def destroy
if @product.destroy
flash[:success] = "Product #{@product.title} deleted"
else
flash[:danger] = "Product #{@product.title} can't be deleted"
end
redirect_to root_url
end
private private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params def product_params
params.require(:product).permit(:title, :sku, :price, :description, params.require(:product).permit(:title, :sku, :price, :description,
:category_id, :image_url) :category_id, :image_url)
end end
def correct_user # Is current user own current editing product?
product = current_user.products.find_by(id: params[:id]) def user_can_edit_product
redirect_to root_url if product.nil? @product = current_user.products.find_by(id: params[:id])
redirect_to root_url, flash: { danger: 'You do not have permission to edit this product' } if @product.blank?
end end
end end
\ No newline at end of file
module ProductsHelper module ProductsHelper
def get_product_thumbnail(product, thumbnail_width, thumbnail_height) def get_product_thumbnail(product, thumbnail_width, thumbnail_height)
# product.image_url always returns PictureUploader object
default_img_path = "product/placeholder_#{thumbnail_width}x#{thumbnail_height}" default_img_path = "product/placeholder_#{thumbnail_width}x#{thumbnail_height}"
product.image_url.present? ? product.image_url : default_img_path # product.image_url always returns PictureUploader object
product.image_url? ? product.image_url : default_img_path
end end
end end
\ No newline at end of file
<li class="product-<%= product.id %>"> <li class="product-<%= product.id %>">
<div class="product product-list"> <div class="product product-list">
<figure class="product-image-area"> <figure class="product-image-area">
<a href="<%= product_url(product) %>"> <%= link_to image_tag(get_product_thumbnail(product, 170, 204)), product_path(product) %>
<%= image_tag(get_product_thumbnail(product, 170, 204)) %>
</a>
</figure> </figure>
<div class="product-details-area"> <div class="product-details-area">
<h2 class="product-name"> <h2 class="product-name">
......
<div class="products-grid columns3">
<% @recommended_products.each do |product| %> <% @recommended_products.each do |product| %>
<li class="product-<%= product.id %>"> <li class="product-<%= product.id %>">
<div class="product"> <div class="product">
<figure class="product-image-area"> <figure class="product-image-area">
<a href="<%= product_url(product) %>"> <%= link_to image_tag(get_product_thumbnail(product, 170, 204)), product_path(product) %>
<%= image_tag(get_product_thumbnail(product, 170, 204)) %>
</a>
</figure> </figure>
<div class="product-details-area"> <div class="product-details-area">
<h2 class="product-name"> <h2 class="product-name">
...@@ -18,4 +15,3 @@ ...@@ -18,4 +15,3 @@
</div> </div>
</li> </li>
<% end %> <% end %>
\ No newline at end of file
</div>
\ No newline at end of file
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
<div class="col-md-9 col-md-push-3 create-product form-section"> <div class="col-md-9 col-md-push-3 create-product form-section">
<h1 class="h2 heading-primary font-weight-normal"> <h1 class="h2 heading-primary font-weight-normal">
Edit Product #<%= @product.id %> Edit Product #<%= @product.id %>
(<%= link_to 'Delete', @product, method: :delete, data: { confirm: 'Are your sure?' } %>)
</h1> </h1>
<div class="featured-box featured-box-primary featured-box-flat featured-box-text-left mt-md"> <div class="featured-box featured-box-primary featured-box-flat featured-box-text-left mt-md">
......
<% flash.each do |message_type, message| %>
<div class="alert alert-<%= message_type %>">
<span><%= message %></span>
</div>
<% end %>
\ No newline at end of file
<div class="container"> <div class="container">
<div class="row"> <div class="row">
<div class="col-md-9 col-md-push-3"> <div class="col-md-9 col-md-push-3">
<%= render 'shared/flash_messages' %>
<h2 class="h2 heading-primary mt-lg clearfix"> <h2 class="h2 heading-primary mt-lg clearfix">
<span>Recommended Items</span> <span>Recommended Items</span>
</h2> </h2>
......
class AddUserRefToProducts < ActiveRecord::Migration[5.1] class AddUserRefToProducts < ActiveRecord::Migration[5.1]
def change def change
add_reference :products, :user, foreign_key: true add_reference :products, :user, index: true
end end
end 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