Class: ProductsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/products_controller.rb

Instance Attribute Summary

Attributes inherited from ApplicationController

#req, #res

Instance Method Summary collapse

Methods inherited from ApplicationController

#boolean_param, #csrf_token, #delete_all_images_from_content, #delete_from_storage, #initialize, #params, #redirect_to, #render, #session, #validate!

Constructor Details

This class inherits a constructor from ApplicationController

Instance Method Details

#createObject



19
20
21
22
23
24
25
26
27
# File 'app/controllers/products_controller.rb', line 19

def create
  data = params['product']
  @product = Product.new(data)
  if @product.save
    redirect_to '/dashboard/products'
  else
    render 'cms/products_form'
  end
end

#destroyObject



43
44
45
46
47
# File 'app/controllers/products_controller.rb', line 43

def destroy
  @product = Product[params['id']]
  @product.destroy
  redirect_to '/dashboard/products'
end

#editObject



29
30
31
32
# File 'app/controllers/products_controller.rb', line 29

def edit
  @product = Product[params['id']]
  render 'cms/products_form'
end

#indexObject



4
5
6
7
8
9
10
11
12
# File 'app/controllers/products_controller.rb', line 4

def index
  if req.path.start_with?('/dashboard')
    @products = Product.order(Sequel.desc(:created_at)).all
    render 'cms/products_index'
  else
    @products = Product.where(is_active: true).order(Sequel.desc(:created_at)).all
    render 'products_index', title: "Shop - One-For-All"
  end
end

#newObject



14
15
16
17
# File 'app/controllers/products_controller.rb', line 14

def new
  @product = Product.new
  render 'cms/products_form'
end

#showObject



49
50
51
52
53
54
55
56
57
# File 'app/controllers/products_controller.rb', line 49

def show
  @product = Product.find(slug: params['slug'], is_active: true)
  if @product
    render 'product_show', title: "#{@product.name} - One-For-All"
  else
    @res.status = 404
    render 'error', message: "Product not found."
  end
end

#updateObject



34
35
36
37
38
39
40
41
# File 'app/controllers/products_controller.rb', line 34

def update
  @product = Product[params['id']]
  if @product.update(params['product'])
    redirect_to '/dashboard/products'
  else
    render 'cms/products_form'
  end
end