• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ruby第五次作业part1(分类、排序)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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
root
'movies#index' 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/


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ruby中的yield的概念发布时间:2022-07-13
下一篇:
Ruby学习: 类的定义和实例变量发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap