1
0
This repository has been archived on 2023-12-07. You can view files and clone it, but cannot push or open issues or pull requests.
body_tracking/app/controllers/units_controller.rb
cryptogopher 628578cda5 Scoped all controllers inside body_trackers
Fixed main menu item highlighting
2019-11-27 22:43:58 +01:00

50 lines
1.0 KiB
Ruby

class UnitsController < ApplicationController
menu_item :body_trackers
before_action :find_project_by_project_id, only: [:index, :create]
before_action :find_unit, only: [:destroy]
before_action :authorize
def index
@unit = @project.units.new
@units = @project.units
end
def create
@unit = @project.units.new(unit_params)
if @unit.save
flash[:notice] = 'Created new unit'
redirect_to project_units_url(@project)
else
@units = @project.units
render :index
end
end
def destroy
# FIXME: do not destroy if anything depends on it
if @unit.destroy
flash[:notice] = 'Deleted unit'
end
redirect_to project_units_url(@project)
end
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_unit
@unit = Unit.find(params[:id])
@project = @unit.project
rescue ActiveRecord::RecordNotFound
render_404
end
end