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/goals_controller.rb
2021-03-07 15:07:45 +01:00

67 lines
1.2 KiB
Ruby

class GoalsController < ApplicationController
layout 'body_tracking', except: :subthresholds
menu_item :body_trackers
helper :body_trackers
include Concerns::Finders
before_action :find_project_by_project_id, only: [:index, :new, :create]
before_action :find_goal, only: [:edit, :update]
before_action :authorize
def index
@goals = @project.goals
end
def new
@goal = @project.goals.new
@targets = @goal.targets
end
def create
@goal = @project.goals.new(goal_params)
if @goal.save
flash.now[:notice] = 'Created new goal'
@goals = @project.goals
else
@targets = @goal.targets
render :new
end
end
def edit
end
def update
if @goal.update(params.require(:goal).permit(:name, :description))
flash.now[:notice] = 'Updated goal'
@goals = @project.goals
else
render :new
end
end
private
def goal_params
params.require(:goal).permit(
:name,
:description,
targets_attributes:
[
:id,
:quantity_id,
:scope,
:destroy,
thresholds_attributes: [
:id,
:quantity_id,
:value,
:unit_id,
:_destroy
]
]
)
end
end