如何按日期对表格数据进行排序?

时间:2014-09-14 19:29:17

标签: ruby-on-rails ruby-on-rails-4

所以,我有一个表格,显示与他们所查看过的剪辑有关的各种用户数据。

现在,我希望能够按桌面上的created_at排序。

当我尝试运行以下代码时,出现以下错误:

NoMethodError in SaasAdmin::StatisticsController#search
undefined method `order' for #<Search:0x0000010a771338>


  def search
    @search = Search.new(params[:search])
    @search_sort = @search.order(params[:sort]) < --- this line causing the error.

search.rb:

class Search < ActiveModel::Name
    attr_accessor :clip, :start_date, :end_date
  extend ActiveModel::Naming
  include ActiveModel::Conversion

  def initialize(params = {})
    params.each do |k, v|
      send("#{k}=", v)
    end
  end

    def start_date=(date)
        @start_date = Date.parse(date)
    end

  def end_date=(date)
    @end_date = Date.parse(date)
  end

  def persisted?
    false
  end
end

statistics_controller:

 class SaasAdmin::StatisticsController < SaasAdminController

  inherit_resources

  def index

  end

  def search
    @search = Search.new(params[:search])
    @search_sort = @search.order(params[:sort])

    @impressions = Impression.where("impressionable_type = 'Clip' AND impressionable_id = ? AND impressions.created_at BETWEEN ? AND ?", @search.clip, @search.start_date, @search.end_date)
    render 'index'
  end

end 

相关的观看代码index.html.erb:

<h1><%= @page_title = 'Statistics' %></h1>
<%#= link_to('Add a new plan', :action => 'new') %>
<%= form_for Search.new, url: statistics_search_saas_admin_statistics_path do |f| %>
<table id="tags" class="table" style="margin-bottom: 5px;">
  <tr>
    <th>Studio</th>
    <th>Film</th>
    <th>Clip</th>
    <th></th>
    <th></th>
  </tr>
  <tr>
  <td><select multiple id="studio_select" style="height: 150px;">
    <% Studio.all.each do |studio| -%>
      <option value="<%=studio.id %>"><%= studio.name %></option>
    <% end -%>
  </select></td>
  <td><select multiple id="film_select" style="height: 150px;">

  </select></td>
  <td><select multiple id="clip_select" name="search[clip]" style="height: 150px;">

  </select></td>
</table>

<table id="tags" class="table" style="margin-bottom: 5px;">
  <tr>
    <th>Beginning Date</th>
    <th>End Date</th>
  </tr>
    <tr>
    <th><%= f.text_field :start_date  %></th>
    <th><%= f.text_field :end_date %></th>
  </tr>
</table>
<%= f.submit "Filter", class: "btn-inverse" %>
<% end -%> 

<% if @impressions %>
<table>
  <tr>
    <th>Studio</th>
    <th>Film</th>
    <th>Clip</th>
    <th>User</th>
    <th>State</th>
    <th>Country</th>
    <th>Type</th>
    <th><%= link_to 'Date', sort: :created_at %></th>
  </tr>  

<% @impressions.each do |impression| -%>
<% @clip = Clip.where(id: impression.impressionable_id).first %>
<% @user = User.where(id: impression.user_id).first %>
    <tr>
    <th>
    <%= link_to @clip.film.studio.name,  saas_admin_studio_path(@clip.film.studio) %></th>
    <th>
    <%= link_to @clip.film.name, saas_admin_studio_film_path(@clip.film.studio, @clip.film) %>
    <th>
    <%= link_to @clip.name, saas_admin_studio_film_clips_path(@clip.film.studio ,@clip.film) %>
    <th>
    <%= link_to @user.username, saas_admin_user_path(@user) %></th>
    <th>
    <%= @user.account.organization.state %></th>
    <th>
    <%= @user.account.organization.country_id %></th>
    <th>
    <%= @user.account.subscription.state %></th>
    <th>
    <%= impression.created_at.strftime("%b %e, %Y %l:%M%P") %></th>
      </tr>
  <% end -%>
  <% end -%>

</table>

<% end -%>

1 个答案:

答案 0 :(得分:0)

为什么要在Search实例上运行排序,而是需要直接在模型上运行int

@search_sort = Search.order(params[:sort])
               ------