Class: MealsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/meals_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#dev_only, #require_login

Methods included from SessionsHelper

#console?, #current_user, #debug?, #forget, #log_in, #log_out, #logged_in?, #remember, #toggle_console, #toggle_debug

Instance Method Details

#calendarObject



49
50
51
52
53
# File 'app/controllers/meals_controller.rb', line 49

def calendar
  my_group_ids = Meal.where(group_id: current_user.groups.map { |m| m.id })

  render json: Meal.convert_to_calendar_json(my_group_ids, current_user.id)
end

#createObject



19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/meals_controller.rb', line 19

def create
  @meal = Meal.create(meals_params)

  if @meal.save
    flash[:success] = "Successfully added meal!"
    redirect_to Group.find(params[:meal][:group_id])
  else
    flash[:danger] = "Failed to add meal!"
    redirect_to Group.find(params[:meal][:group_id])
  end
end

#destroyObject



42
43
44
45
46
47
# File 'app/controllers/meals_controller.rb', line 42

def destroy
  @meal = Meal.find(params[:id])
  @meal.destroy
  
  redirect_to meals_path
end

#editObject



15
16
17
# File 'app/controllers/meals_controller.rb', line 15

def edit
  @meal = Meal.find(params[:id])
end

#indexObject



3
4
5
# File 'app/controllers/meals_controller.rb', line 3

def index
  @meals = Meal.all
end

#newObject



11
12
13
# File 'app/controllers/meals_controller.rb', line 11

def new
  @meal = Meal.new
end

#showObject



7
8
9
# File 'app/controllers/meals_controller.rb', line 7

def show
  @meal = Meal.find(params[:id])
end

#updateObject



31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/meals_controller.rb', line 31

def update
  @meal = Meal.find(params[:id])

  if @meal.update(meals_params)
    flash[:success] = "Successfully updated meal!"
    redirect_to @meal
  else
    render 'edit'
  end
end