简体   繁体   English

Rails-用户选择的过滤器

[英]Rails - user selected filters

I have a model Shop, database with attributes: shop name,year the shop was opened, total sales (and few more). 我有一个商店模型,该数据库具有以下属性:商店名称,商店开业年限,总销售额(还有更多)。 I have done some statistics on this data and result of it is this table: 我已经对该数据进行了一些统计,其结果是此表:

在此处输入图片说明

Now, what I want is to have the user select/enter the year, and the app to show the below data just for that year (instead of the whole table) 现在,我要让用户选择/输入年份,并且该应用程序仅显示该年份的以下数据(而不是整个表格)

My shop.rb for table part: 我的shop.rb表部分:

class Shop < ActiveRecord::Base
  belongs_to :opened
  def self.percentile(values, percentile)
    values_sorted = values.sort
    k = (percentile*(values_sorted.length-1)+1).floor - 1
    f = (percentile*(values_sorted.length-1)+1).modulo(1)
    return values_sorted[k] + (f * (values_sorted[k+1] - values_sorted[k]))
  end

  def self.median_by_year
    arr ||= []
    years = Shop.distinct.pluck(:year).sort
    years.each do |yr|
      sales =  Shop.where(year: yr).pluck(:items_sold)
      perc = percentile(sales, 0.5)
      arr << perc.to_i
    end
    return arr
  end
end

my controller: 我的控制器:

class ShopsController < ApplicationController
  def index
    @count = Shop.count
    @sales_average = Shop.average(:items_sold).to_i

    @sold = Shop.pluck(:items_sold)
    @top_twenty_sales_average = Shop.percentile(@sold, 0.80).to_i
    @bottom_twenty_sales_average = Shop.percentile(@sold, 0.20).to_i
    @median = Shop.percentile(@sold, 0.50).to_i

    @average_sales_by_year = Shop.select('year, count(names) as count').group('year').order('year').average('items_sold')
    @min_sales_by_year = Shop.select('year, count(names) as count').group('year').order('year').minimum('items_sold')

    @med = Shop.median_by_year
    @table = @average_sales_by_year.zip@med.zip@min_sales_by_year
  end

and finally table in the view file 最后是视图文件中的表

<table>
    <thead>
        <tr>
            <th>Year</th>
            <th>Average sales</th>
            <th>Median sales</th>
            <th>Min sales</th>
        </tr>
    </thead>
    <tbody>
        <% @table.each do |avg, med| %>
        <tr>
            <td><%= avg[0] %></td>
            <td><%= avg[1].to_i.to_s %></td>
            <td><%= med[0] %></td>
            <td><%= med[1][1] %></td>
        </tr>
        <% end %>
    </tbody>
</table>

In order to create filter I tried bunch of stuff based on similar SO questions, but this is my first app ever, so basically I don't understand enough to know what to do. 为了创建过滤器,我基于类似的SO问题尝试了一堆东西,但这是我有史以来的第一个应用程序,因此基本上我不了解要做什么。 So far I tried to add this to the model: 到目前为止,我尝试将其添加到模型中:

  def self.by_year(selected_year=nil)
    if year
      where(year: selected_year)
    else
      scoped
    end
  end

this to the controller: 这给控制器:

@shops = Shop.select(params[:year])

and finally this in the view: 最后在视图中:

<%= form_tag url: shops_path do %>
        <%= select_tag :year  %>
        <%= submit_tag 'Filter' %>
<% end %>

but I don't really know if that is right, or how to continue. 但我真的不知道这是对的还是继续。 Any help will be greatly appreciated. 任何帮助将不胜感激。

it would probably be cleaner to create a view for your table and than have a separate model of that view with the year scope 为表创建视图可能比使用年份范围的视图单独模型更干净

class ShopStats < ActiveRecord::Base
   scope :for_year, ->(year) { where(:year => year)}
end

than in your controller you can have: 比您的控制器可以拥有:

ShopStats.for_year params[:year]

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

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