简体   繁体   English

Ruby on Rails:排序和突出显示

[英]Ruby on Rails : Sorting and highlighting

This is the index function in movies.controller.rb file. 这是movie.controller.rb文件中的索引函数。

 def index
         @movies = Movie.find(:all, :order => (params[:sort_by]))
         @sort_column = params[:sort_by]
      end

This is the view file. 这是视图文件。 -# This file is app/views/movies/index.html.haml %h1 All Movies -#此文件是app / views / movies / index.html.haml%h1所有电影

%table#movies
  %thead
    %tr
      %th{:class=>("hilite" if @sort_column == "title")}= link_to 'Movie Title', movies_path(:sort_by=>'title'), :id=>"title_header"     
      %th Rating
      %th{:class=>("hilite" if @sort_column == "release_date")}= link_to 'Release Date', movies_path(:sort_by=>'release_date'), :id=>"release_date_header"
      %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

default.css default.css

table#movies th.hilite {
  background-color: yellow;
}

It gives me the following error in the first line of index: 它在索引的第一行给了我以下错误:

Couldn't find all Movies with 'id': (all, {:order=>nil})

I need to sort the list of movies by title and release date and also highlight it yellow. 我需要按标题和发行日期对电影列表进行排序,并以黄色突出显示。 But, with this code, neither is it getting highlighted, nor sorted. 但是,使用此代码,它既不会突出显示,也不会排序。 Where lies the error? 错误在哪里?

If you are using Rails 3 or 4, the find command has changed. 如果使用的是Rails 3或4,则find命令已更改。 It no longer takes a symbol as it's first argument (read more here: http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find ) 它不再以符号作为第一个参数(在此处了解更多信息: http : //api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find

The Rails 3 or 4 equivalent would be: Rails 3或4等效于:

@movies = Movie.order(params[:sort_by])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM