From 25e6039a9fce4cec42451e7bdf482a2edc9caca7 Mon Sep 17 00:00:00 2001 From: yamin Date: Mon, 21 Aug 2017 16:12:59 +0630 Subject: [PATCH] CRUDcommissioner --- app/controllers/commissioners_controller.rb | 5 ++- app/models/commissioner.rb | 1 + app/models/employee.rb | 2 +- app/views/commissioners/_form.html.erb | 4 ++ app/views/commissioners/edit.html.erb | 16 ++++--- app/views/commissioners/index.html.erb | 49 +++++++++++++++------ app/views/commissioners/new.html.erb | 15 ++++--- app/views/commissioners/show.html.erb | 41 +++++++++++++++-- app/views/layouts/_header.html.erb | 1 + config/routes.rb | 4 +- 10 files changed, 105 insertions(+), 33 deletions(-) diff --git a/app/controllers/commissioners_controller.rb b/app/controllers/commissioners_controller.rb index a1204de2..b0c1b2a8 100644 --- a/app/controllers/commissioners_controller.rb +++ b/app/controllers/commissioners_controller.rb @@ -15,6 +15,7 @@ class CommissionersController < ApplicationController # GET /commissioners/new def new @commissioner = Commissioner.new + @employee = Employee.all.order('name asc') end # GET /commissioners/1/edit @@ -25,7 +26,7 @@ class CommissionersController < ApplicationController # POST /commissioners.json def create @commissioner = Commissioner.new(commissioner_params) - + @commissioner.created_by = current_user.id respond_to do |format| if @commissioner.save format.html { redirect_to @commissioner, notice: 'Commissioner was successfully created.' } @@ -69,6 +70,6 @@ class CommissionersController < ApplicationController # Never trust parameters from the scary internet, only allow the white list through. def commissioner_params - params.fetch(:commissioner, {}) + params.require(:commissioner).permit(:name,:emp_id,:created_by,:commission_type, :is_active) end end diff --git a/app/models/commissioner.rb b/app/models/commissioner.rb index 057a94d0..c6d4ab4c 100644 --- a/app/models/commissioner.rb +++ b/app/models/commissioner.rb @@ -1,2 +1,3 @@ class Commissioner < ApplicationRecord + has_many :employees end diff --git a/app/models/employee.rb b/app/models/employee.rb index 85aeaec1..a6b52f21 100644 --- a/app/models/employee.rb +++ b/app/models/employee.rb @@ -1,6 +1,6 @@ class Employee < ApplicationRecord has_secure_password - + belongs_to :commissioner has_many :shit_sales validates_presence_of :name, :role validates_presence_of :password, :on => [:create] diff --git a/app/views/commissioners/_form.html.erb b/app/views/commissioners/_form.html.erb index 0ad6098b..6768b139 100644 --- a/app/views/commissioners/_form.html.erb +++ b/app/views/commissioners/_form.html.erb @@ -2,6 +2,10 @@ <%= f.error_notification %>
+ <%= f.input :name %> + <%= f.collection_select :emp_id, Employee.all.order('name asc'), :emp_id, :name, {prompt: "Select an Employee"}, {class: "form-control"} %> + <%= f.input :commission_type %> +
diff --git a/app/views/commissioners/edit.html.erb b/app/views/commissioners/edit.html.erb index 9f5edfe0..55f50020 100644 --- a/app/views/commissioners/edit.html.erb +++ b/app/views/commissioners/edit.html.erb @@ -1,6 +1,10 @@ -

Editing Commissioner

- -<%= render 'form', commissioner: @commissioner %> - -<%= link_to 'Show', @commissioner %> | -<%= link_to 'Back', commissioners_path %> +
+ + <%= render 'form', commissioner: @commissioner %> +
\ No newline at end of file diff --git a/app/views/commissioners/index.html.erb b/app/views/commissioners/index.html.erb index 82080871..9fdaa90a 100644 --- a/app/views/commissioners/index.html.erb +++ b/app/views/commissioners/index.html.erb @@ -1,25 +1,46 @@ -

<%= notice %>

+ -

Commissioners

- - - +
+
+
+ + + + + + - + - + <% @commissioners.each do |commissioner| %> - + + + + + + - + <% end %> -
NameEmployee NameCommission typeActiveCreated By
<%= link_to 'Show', commissioner %><%= commissioner.name %> + <% if Employee.exists? %> + <% employee = Employee.where('emp_id=?',commissioner.emp_id) %> + <%= employee[0].name %> + <% end %> + + <%= commissioner.commission_type %><%= commissioner.is_active %><%= current_user.name %><%= link_to 'Show', commissioner_path(commissioner) %> <%= link_to 'Edit', edit_commissioner_path(commissioner) %><%= link_to 'Destroy', commissioner, method: :delete, data: { confirm: 'Are you sure?' } %><%= link_to 'Destroy', commissioner_path(commissioner), method: :delete, data: { confirm: 'Are you sure?' } %>
- -
- -<%= link_to 'New Commissioner', new_commissioner_path %> + +
diff --git a/app/views/commissioners/new.html.erb b/app/views/commissioners/new.html.erb index 21a4e77f..6cadebcc 100644 --- a/app/views/commissioners/new.html.erb +++ b/app/views/commissioners/new.html.erb @@ -1,5 +1,10 @@ -

New Commissioner

- -<%= render 'form', commissioner: @commissioner %> - -<%= link_to 'Back', commissioners_path %> +
+ + <%= render 'form', commissioner: @commissioner %> +
\ No newline at end of file diff --git a/app/views/commissioners/show.html.erb b/app/views/commissioners/show.html.erb index 2ec503e3..ba313550 100644 --- a/app/views/commissioners/show.html.erb +++ b/app/views/commissioners/show.html.erb @@ -1,4 +1,39 @@ -

<%= notice %>

+ +
+
+

Commissioner

+ + + + + + + + + + + + + + + + +
Name<%= @commissioner.name %>
Employee Name + <% if Employee.exists? %> + <% employee = Employee.where('emp_id=?',@commissioner.emp_id) %> + <%= employee[0].name %> + <% end %> + +
Commission Type<%= @commissioner.commission_type %>
Active<%= @commissioner.is_active %>
Created By<%= current_user.name %>
<%= link_to 'Edit', edit_commissioner_path(@commissioner) %><%= link_to 'Destroy', commissioner_path(@commissioner), method: :delete, data: { confirm: 'Are you sure?' } %>
+
+
+ + diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 0daa8f5f..364f8af2 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -28,6 +28,7 @@
  • <%= link_to "Print Setting", print_settings_path, :tabindex =>"-1" %>

  • <%= link_to "Employees", settings_employees_path, :tabindex =>"-1" %>
  • +
  • <%= link_to "Commissioners", commissioners_path, :tabindex =>"-1" %>

  • <%= link_to "Accounts", settings_accounts_path, :tabindex =>"-1" %>

  • diff --git a/config/routes.rb b/config/routes.rb index 5eccfd36..ff0f6ba9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,7 @@ require 'sidekiq/web' Rails.application.routes.draw do - resources :commissioners + root 'home#index' mount Sidekiq::Web => '/kiq' @@ -323,6 +323,6 @@ Rails.application.routes.draw do #----------- Print Setup --------# resources :print_settings - + resources :commissioners # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end