forked from prusswan/hw2_rottenpotatoes
/
movies_controller.rb
59 lines (49 loc) · 1.53 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
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
# raise session.inspect
@all_ratings = Movie.get_ratings
if !params[:ratings]
redirect_to(movies_path({:ratings => session[:ratings], :sort => params[:sort]}))
elsif !params[:sort]
redirect_to(movies_path({:ratings => params[:ratings], :sort => session[:sort]}))
else
@checked_ratings = params[:ratings]
session[:ratings] = @checked_ratings
session[:sort] = params[:sort]
@movies = Movie.where(:rating => params[:ratings].keys)
end
if params[:sort] == "title"
@movies = @movies.sort_by {|m| m.title}
elsif params[:sort] == "date"
@movies = @movies.sort_by {|m| m.release_date}
end
end
def new
# default: render 'new' template
end
def create
@movie = Movie.create!(params[:movie])
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!(params[:movie])
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
end