Class: SuppliersDistributorsController

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

Instance Method Summary collapse

Instance Method Details

#createObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/suppliers_distributors_controller.rb', line 16

def create
  if params["supplier"] != nil 
    @supplier = Supplier.new(supplier_distributor_params("supplier"))
    if @supplier.save
      redirect_to :action => 'index'
    else
      flash[:alert] = @supplier.errors.full_messages.to_sentence
      redirect_to :action => 'new'
    end
  else
    @distributor = Distributor.new(supplier_distributor_params("distributor"))
    if @distributor.save
      redirect_to :action => 'index'
    else
      flash[:alert] = @distributor.errors.full_messages.to_sentence
      redirect_to :action => 'new'
    end
  end
end

#def(destroy) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/suppliers_distributors_controller.rb', line 73

def 

def destroy
  if params[:type] == "supplier"
    @supplier = Supplier.find(params[:id])
    @supplier.destroy!
    redirect_to :action => 'index', :notice => "#{@supplier.name} has been deleted"
  else
    @distributor = Distributor.find(params[:id])
    @distributor.destroy!
    redirect_to :action => 'index', :notice => "#{@distributor.name} has been deleted"
  end
end

#editObject



36
37
38
39
40
41
42
43
44
# File 'app/controllers/suppliers_distributors_controller.rb', line 36

def edit
  if params[:type] == "supplier" 
    @supplier = Supplier.find(params[:id])
    @type = "supplier"
  else
    @distributor = Distributor.find(params[:id])
    @type == "distributor"
  end
end

#indexObject



2
3
4
5
# File 'app/controllers/suppliers_distributors_controller.rb', line 2

def index
  @distributors = Distributor.page(params[:page]).per(5)
  @suppliers = Supplier.page(params[:page]).per(5)
end

#newObject



7
8
9
10
11
12
13
14
# File 'app/controllers/suppliers_distributors_controller.rb', line 7

def new
  @type = params[:type]
  if @type == "supplier" 
    @supplier = Supplier.new
  else
    @distributor = Distributor.new
  end
end

#showObject



70
71
# File 'app/controllers/suppliers_distributors_controller.rb', line 70

def show
end

#supplier_distributor_params(type) ⇒ Object



87
88
89
90
91
92
93
# File 'app/controllers/suppliers_distributors_controller.rb', line 87

def supplier_distributor_params type
  if type == "supplier" || params["supplier"] != nil
    params.require(:supplier).permit(:name)
  else
    params.require(:distributor).permit(:name)
  end
end

#updateObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/suppliers_distributors_controller.rb', line 46

def update
 if params["supplier"] != nil
   @supplier = Supplier.find params[:id]
   if @supplier.update (supplier_distributor_params params[:type])
     flash[:message] = "Update to supplier was successful"
     redirect_to :action => 'index'
   else
     flash[:alert] = @supplier.errors.full_messages.to_sentence
     redirect_to :action => 'edit'
   end
 else
   @distributor = Distributor.find params[:id]
   if @distributor.update (supplier_distributor_params params[:type])
     flash[:message] = "Update to supplier was successful"
     redirect_to :action => 'index'
   else
     flash[:alert] = @distributor.errors.full_messages.to_sentence
     redirect_to :action => 'edit'
   end
 end
 
 
end