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

   2023-02-09 学习力0
核心提示:movies_controller.rbclass MoviesControllerApplicationControllerdef movie_paramsparams.require(:movie).permit(:title, :rating, :description, :release_date)enddef showid = params[:id] # retrieve movie ID from URI route@movie = Movie.find(id)

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举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • [ruby on rails] 跟我学之(6)显示指定数据
    根据《[ruby on rails] 跟我学之路由映射》,我们知道,可以访问 GET    /posts/:id(.:format) 来显示具体的对象。 修改 app/controllers/posts_controller.rb的show这个action。这里有个难题,如果获取url里面的参数?可以通过params内置变量进行访问
    03-16
  • [ruby on rails] 跟我学之(10)数据输入验证
    这里简单加上几个验证,非空,最小长度,唯一修改app/models/post.rb文件,如下:class PostActiveRecord::Base#attr_accessible :title, :contentvalidates :title, :context, :presence = truevalidates :title, :length = { :minimum =2}validates :title,
    03-16
  • 我尝试使用 Ruby 和 mittsu 库制作 3D 战斗动作游戏
    我尝试使用 Ruby 和 mittsu 库制作 3D 战斗动作
    概述这篇文章是关于我偶然发现的 hitbox 的回忆录。2022 夏季鲁比训练营创造了这个游戏。我的游戏仓库这里是。我们作为一个团队开发,有 4 名成员第一次见面,并第一次使用 Git 和 Github 进行开发。使用的技术是Ruby,三通图书馆是。我主要负责碰撞检测,所
    03-16
  • 我只是想在我的 Mac 上将 Ruby 和 Rails 更新到终端中的最新版本。 .
    我只是想在我的 Mac 上将 Ruby 和 Rails 更新到
    介绍自从我尝试创建一个简单的应用程序以来已经有很长时间了,并且我尝试在创建它之前将 Ruby 和 Rails 更新到最新版本,但是我意外卡住了,所以我将它作为备忘录留下。作为版本升级1. 更新 Homebrew 和 rbenv2. 红宝石更新3. Rails 更新这就是它的感觉。让我
    03-16
  • ruby写爬虫 ruby python
    ruby写爬虫 ruby python
    http://www.javaeye.com/topic/545160爬虫性能比较http://www.rubyrailways.com/data-extraction-for-web-20-screen-scraping-in-rubyrails/srcapihttp://huacnlee.com/blog/ruby-scrapi-collect-koubei  2009年4月22日 星期三用ruby写的一个网络爬虫程序前
    03-08
  • 一个关于创建一个可以用 Ruby 做 UMAP 的 gem 的故事
    一个关于创建一个可以用 Ruby 做 UMAP 的 gem
    介绍统一流形逼近和投影 (UMAP) 是一种通过降维的可视化方法,通常与 t-SNE 一起使用。用 Ruby 语言执行机器学习时,瘤胃我认为有很多情况下你使用 gem 调用。瘤胃有t-SNE但不是UMAP。这一次,它是一个 C++ 库乌马普的红宝石绑定我创造了它,所以我会在我忘记
    03-08
  • 让我们制作一个应用程序,当您在表格①中输入名称时输出 PDF 戳记(使用 Ruby on Rails 创建 PDF 文件)
    让我们制作一个应用程序,当您在表格①中输入名
    介绍你好!我的名字是荣查本。这次在Ruby on Rails中实现PDF转换的时候,用到了一个叫Prawn的gem,所以总结了一下。您可以通过阅读本文创建フォームに名前を入力するとPDF化された印鑑を出力するアプリ。即使是我,一个初学者也可以做到,所以实现方法很简单
    03-08
  • [脚本_Ruby]Windows安装配置Ruby On Rails
    感觉Java学的差不多了,想接触下Ruby On Rails,看看它比Java WEB高效到哪里了,在Ubuntu12.10上弄了两天总是报错提示没有指向的文件sqlite3,不管怎么安装sqlite3都不行,到最后没办法只有跑到WIN7平台下试试,以下就是我搭建Ruby On Rails的过程:    
    02-10
  • Prawn:Ruby生成PDF更简捷的选择
    在InfoQ上看到《Prawn:使用Ruby生成PDF更简捷》,其说到的Prawn可以更加快捷的在Ruby中生成PDF文件。因为之前使用过很多版本的PDF生成类库都不尽如人意,有的太复杂,有的太慢,于是对这个做了测试。1、安装安装很简单,直接使用gem install prawn即可安装完
    02-10
  • Ruby On Rails:InstanRails
    参考数据:对于Ruby On Rails 不是粉清楚的朋友可以参考以下的连结信息,该连结网站都提供不错的Ruby On Rails 信息。Ruby: 一个纯OO的脚本语言..Ruby on Rails: 快速建置Web的MVC架构的Framework说明:整合环境: Instant Rails 是在Windows环境中,整合了Ruby,
    02-10
点击排行