class MoviesController < ApplicationController def movie_params params.require(:movie).permit(:title, :rating, :description, :release_date) end 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. by default end def index sort = params[:sort] || session[:sort] if sort == :title or sort == :release_date session[:sort] = sort || {} end @title_header = 'hilite' if sort == "title" @release_date_header = 'hilite' if sort == "release_date" all_ratings = Movie.all.select(:rating).group(:rating).order(:rating) @all_ratings = all_ratings.map {|d| d.rating} if params[:ratings] != nil session[:ratings] = params[:ratings].map {|i, v| i} @ratings_array = session[:ratings] else if params[:sort] == nil && params[:controller] == :movies @ratings_array = @all_ratings session[:ratings] = @ratings_array || @all_ratings else @ratings_array = session[:ratings] || @all_ratings || {} end end if params[:sort] != session[:sort] or params[:ratings] != session[:ratings] session[:sort] = sort session[:ratings] = @ratings_array if @ratings_array == {} redirect_to :sort => sort, :ratings => @all_ratings and return else redirect_to :sort => sort, :ratings => @ratings_array and return end end @movies = Movie.all.where(rating: @ratings_array) @movies = @movies.order(sort) 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 end