在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
movies_controller.rb 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.<extension> by default end def index @all_ratings = Array.new Movie.select(:rating).distinct.each do |movie| @all_ratings.push movie.rating end @ratings = params[:ratings] ? params[:ratings].keys : @all_ratings @ratings.delete("hidden") @movies = Movie.where(:rating => @ratings) end def sort index @order = params[:order] if !@order #如果order参数不存在,默认增序 @order = "increase" end @keyword = params[:keyword] if @keyword #是对哪个关键字排序 @movies = @movies.order("#{@keyword}#{@order == 'increase' ? '' : ' DESC'}") @order = @order == "increase" ? "decrease" : "increase" end render 'movies/index' 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
index.html.haml -# This file is app/views/movies/index.html.haml %h1 All Movies = form_tag movies_path, :method => :get do Include: - @all_ratings.each do |rating| = rating = check_box_tag "ratings[#{rating}]", "1", (@ratings.include? rating) = check_box_tag "ratings[hidden]", "1", true, hidden:true = submit_tag 'Refresh' %table#movies %thead - sorted_css = @order ? " sorted-#{@order}" : "" - @order = @order ? @order : "increase" %tr %th{:class => "linked#{@keyword == 'title' ? sorted_css : ''}"}= link_to "Movie Title", movies_sort_path(:keyword => 'title', :order => @order, :ratings => params[:ratings]) %th Rating %th{:class => "linked#{@keyword == 'release_date' ? sorted_css : ''}"}= link_to "Release Date", movies_sort_path(:keyword => 'release_date', :order => @order, :ratings => params[:ratings]) %th More Info %tbody - @movies.each do |movie| %tr %td= movie.title %td= movie.rating %td= movie.release_date %td= link_to "More about #{movie.title}", movie_path(movie) = link_to 'Add new movie', new_movie_path routes.rb Rails.application.routes.draw do get 'movies/sort', to: 'movies#sort' resources :movies do end
default.css添加以下样式 table#movies th.linked { padding: 0px; } table#movies th a { padding: 4px; display: block; } table#movies th a:hover { background-color:rgb(255, 125, 0); cursor: pointer; } table#movies th.sorted-increase { background-color:rgb(255, 165, 0); } table#movies th.sorted-decrease { background-color:rgb(255, 235, 0); }
网址链接:http://ucas-jec-new.herokuapp.com/ |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论