-
Notifications
You must be signed in to change notification settings - Fork 0
/
movies_controller.rb
69 lines (61 loc) · 1.95 KB
/
movies_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class MoviesController < ApplicationController
def show
id = params[:id] # retrieve movie ID from URI route
@movie = Movie.find(id) # look up movie by unique ID
# will render app/views/movies/show.<extension> by default
end
def index
sort = params[:sort] || session[:sort]
case sort
when 'title'
ordering, @title_header = {:title => :asc}, 'hilite'
when 'release_date'
ordering, @date_header = {:release_date => :asc}, 'hilite'
end
@all_ratings = Movie.all_ratings
@checked_ratings = params[:ratings] || session[:ratings]
if @checked_ratings == nil
ratings = Movie.all_ratings
session[:ratings] = Movie.all_ratings
else
ratings = @checked_ratings.keys
if (session[:sort] != params[:sort]) || (session[:ratings] != params[:ratings])
session[:sort] = sort
session[:ratings] = @checked_ratings
redirect_to :sort => sort, :ratings => @checked_ratings
return
end
end
@movies = Movie.order(ordering).where(rating: ratings)
@check = ratings
end
def new
# default: render 'new' template
end
def create
@movie = Movie.create!(movie_params)
flash[:notice] = "#{@movie.title} was successfully created."
redirect_to movies_path
end
def edit
@movie = Movie.find params[:id]
end
def update
@movie = Movie.find params[:id]
@movie.update_attributes!(movie_params)
flash[:notice] = "#{@movie.title} was successfully updated."
redirect_to movie_path(@movie)
end
def destroy
@movie = Movie.find(params[:id])
@movie.destroy
flash[:notice] = "Movie '#{@movie.title}' deleted."
redirect_to movies_path
end
private
# Making "internal" methods private is not required, but is a common practice.
# This helps make clear which methods respond to requests, and which ones do not.
def movie_params
params.require(:movie).permit(:title, :rating, :description, :release_date)
end
end