From b503f91e3b68ad35c92481a90613e6d36c44383a Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Thu, 22 Aug 2019 17:47:32 +0200 Subject: [PATCH] Implemented UnitsController#create/destroy --- app/controllers/units_controller.rb | 27 +++++++++++++++++++++++++++ app/views/units/index.html.erb | 6 +++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/app/controllers/units_controller.rb b/app/controllers/units_controller.rb index eab0563..43439ce 100644 --- a/app/controllers/units_controller.rb +++ b/app/controllers/units_controller.rb @@ -1,5 +1,6 @@ class UnitsController < ApplicationController before_action :find_project, only: [:index, :create, :import] + before_action :find_unit, only: [:destroy] before_action :authorize def index @@ -8,9 +9,21 @@ class UnitsController < ApplicationController end def create + @unit = Unit.new(unit_params.update(project: @project)) + if @unit.save + flash[:notice] = 'Created new unit' + redirect_to project_units_url(@project) + else + @units = @project.units + render :index + end end def destroy + if @unit.destroy + flash[:notice] = 'Deleted unit' + end + redirect_to project_units_url(@project) end def import @@ -23,6 +36,13 @@ class UnitsController < ApplicationController private + def unit_params + params.require(:unit).permit( + :name, + :shortname + ) + end + # :find_* methods are called before :authorize, # @project is required for :authorize to succeed def find_project @@ -30,4 +50,11 @@ class UnitsController < ApplicationController rescue ActiveRecord::RecordNotFound render_404 end + + def find_unit + @unit = Unit.find(params[:id]) + @project = @unit.project + rescue ActiveRecord::RecordNotFound + render_404 + end end diff --git a/app/views/units/index.html.erb b/app/views/units/index.html.erb index 61f7d3f..5a52667 100644 --- a/app/views/units/index.html.erb +++ b/app/views/units/index.html.erb @@ -11,12 +11,12 @@ <% end %> -