diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 9f6da93..04815d5 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -5,11 +5,11 @@ class MoviesController < ApplicationController def index # Fetch sorting parameters from the URL - sort_attribute = params[:attribute] || 'title' - sort_order = params[:order] || 'asc' + @attribute = params[:attribute] || 'title' + @order = params[:order] || 'asc' # Fetch and sort movies based on parameters - @movies = Movie.sorted_by(sort_attribute, sort_order) + @movies = Movie.sorted_by(@attribute, @order) end # GET /movies/1 or /movies/1.json @@ -25,9 +25,7 @@ def new def edit end - def sort - - end + # POST /movies or /movies.json def create @movie = Movie.new(movie_params) @@ -58,10 +56,11 @@ def update # DELETE /movies/1 or /movies/1.json def destroy + @movie = Movie.find(params[:id]) @movie.destroy! respond_to do |format| - format.html { redirect_to movies_url, notice: "Movie was successfully destroyed." } + format.html { redirect_to movies_path(attribute: @attribute, order: @order), notice: "Movie was successfully destroyed." } format.json { head :no_content } end end @@ -77,9 +76,4 @@ def movie_params params.require(:movie).permit(:title, :rating, :description, :release_date) end - def sort - respond_to do |format| - format.html { redirect_to movies_url, notice: "Sorting is yet to be done" } - end - end end diff --git a/app/views/movies/_movie.html.erb b/app/views/movies/_movie.html.erb index ab46c0c..1553a27 100644 --- a/app/views/movies/_movie.html.erb +++ b/app/views/movies/_movie.html.erb @@ -2,9 +2,15 @@
+ | + <%= link_to 'Title', movies_path(attribute: 'title', order: params[:order] == 'asc' ? 'desc' : 'asc') %> <% if params[:attribute] == 'title' %> <%= params[:order] == 'asc' ? '▲' : '▼' %> <% end %> | -+ | <%= link_to 'Rating', movies_path(attribute: 'rating', order: params[:order] == 'asc' ? 'desc' : 'asc') %> <% if params[:attribute] == 'rating' %> <%= params[:order] == 'asc' ? '▲' : '▼' %> <% end %> | -+ | <%= link_to 'Release Date', movies_path(attribute: 'release_date', order: params[:order] == 'asc' ? 'desc' : 'asc') %> <% if params[:attribute] == 'release_date' %> <%= params[:order] == 'asc' ? '▲' : '▼' %> @@ -87,12 +129,37 @@ | ||
---|---|---|---|---|---|---|---|
<%= link_to "New movie", new_movie_path %> | +<%= link_to "New movie", new_movie_path(attribute: params[:attribute], order: params[:order]) %> |
<%= notice %>
+ + + + + + + + + + +<%= notice %>
<%= render partial: 'movieshow', locals: { movieshow: @movie } %> +