diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 04815d5..2682737 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -4,12 +4,10 @@ class MoviesController < ApplicationController # GET /movies or /movies.json def index - # Fetch sorting parameters from the URL @attribute = params[:attribute] || 'title' @order = params[:order] || 'asc' - # Fetch and sort movies based on parameters - @movies = Movie.sorted_by(@attribute, @order) + @movies = Movie.sort_based(@attribute, @order) end # GET /movies/1 or /movies/1.json @@ -23,16 +21,18 @@ def new # GET /movies/1/edit def edit + @movie = Movie.find(params[:id]) end # POST /movies or /movies.json + + def create @movie = Movie.new(movie_params) - respond_to do |format| if @movie.save - format.html { redirect_to movie_url(@movie), notice: "Movie was successfully created." } + format.html { redirect_to movies_path(attribute: params[:attribute], order: params[:order]), notice: "Movie was successfully created." } format.json { render :show, status: :created, location: @movie } else format.html { render :new, status: :unprocessable_entity } @@ -41,6 +41,7 @@ def create end end + # PATCH/PUT /movies/1 or /movies/1.json def update respond_to do |format| diff --git a/app/models/movie.rb b/app/models/movie.rb index 456c12a..b5dbc67 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,14 +1,5 @@ class Movie < ActiveRecord::Base - # Class method to sort movies based on the attribute and order - def self.sorted_by(attribute, order) - valid_attributes = %w[title rating release_date] # Allowed attributes for sorting - valid_order = %w[asc desc] # Allowed orders - - # Default values if parameters are invalid - attribute = 'title' unless valid_attributes.include?(attribute) - order = 'asc' unless valid_order.include?(order) - - # Apply sorting using ActiveRecord's `order` method + def self.sort_based(attribute, order) order("#{attribute} #{order}") end end diff --git a/app/views/movies/_movie.html.erb b/app/views/movies/_movie.html.erb index 1553a27..891b120 100644 --- a/app/views/movies/_movie.html.erb +++ b/app/views/movies/_movie.html.erb @@ -2,13 +2,13 @@
- + <%= movie.title %> - + <%= movie.rating %> - + <%= movie.release_date %> <%= link_to "Click here", movie %> diff --git a/app/views/movies/edit.html.erb b/app/views/movies/edit.html.erb index 9ecbf5f..eb61111 100644 --- a/app/views/movies/edit.html.erb +++ b/app/views/movies/edit.html.erb @@ -1,51 +1,40 @@ - - - - - - - - - - - Simple Styled Page + + +
- <% content_for :title, "Editing movie" %> + <% content_for :title, "EDIT MOVIE" %>

Editing movie

diff --git a/app/views/movies/index.html.erb b/app/views/movies/index.html.erb index c2a8cea..c0f8edc 100644 --- a/app/views/movies/index.html.erb +++ b/app/views/movies/index.html.erb @@ -2,22 +2,25 @@ <% content_for :title, "Movies" %> -

Movies

+

MOVIES

-

New movie

+

NEW MOVIE

diff --git a/app/views/movies/show.html.erb b/app/views/movies/show.html.erb index 7ed2c8e..a562b67 100644 --- a/app/views/movies/show.html.erb +++ b/app/views/movies/show.html.erb @@ -1,50 +1,39 @@ - - - - - - - - - - Simple Styled Page
-

Show Movie

+

SHOW MOVIE

@@ -67,4 +56,3 @@ -