1
0

Implemented UnitsController#create/destroy

This commit is contained in:
cryptogopher
2019-08-22 17:47:32 +02:00
parent e9fc9c6475
commit b503f91e3b
2 changed files with 30 additions and 3 deletions

View File

@@ -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