如何在控制器和视图中使用范围:使用Rails中的范围按类别(枚举)过滤表

时间:2019-05-24 21:18:52

标签: ruby-on-rails ajax filtering scopes

我的rails应用程序中有一个html表,在其中我应负担所有费用。费用是一个枚举,可以取诸如“教育”,“啤酒”,“ bliblablub”之类的值。

我希望能够根据这些枚举值过滤html表。我在表格的侧面有链接,用户可以单击。现在,我希望表格根据链接过滤结果。

现在,我认为示波器是解决问题的方法,但是我很难理解它们的真正工作原理。我创建了这样一个范围只是为了了解它:

scope :find_category, -> {Expense.where(category:"education")}

在Rails控制台中,此方法有效,当我调用find_category方法时,它为我提供了具有教育意义的实例。

我认为,动态范围将类似于:

scope :find_category, -> (category) {Expense.where(category:category)}

到目前为止,一切都很好。现在,我没有真正得到的是如何在控制器和视图中使用过滤后的结果,即单击链接时如何对其进行过滤。

我尝试过:

控制器(试图获取我查询的结果)

  def find_category
    @expense = Expense.find_category
    render action: :index
  end

然后为find_categories放置一条路线:

  resources :expenses, path: 'expenses' do
    collection do
      get :find_category
    end
  end

并在索引中放置一个链接:

 <%= link_to 'education', {controller: 'expenses', action: 'index', :find_category => 'education'}  %>

我知道这不是真的。单击链接虽然可以给我expenses?find_category=education。但是什么都没有改变。因此,我努力寻找正确的方法。当然,在不重新加载页面的情况下这样做也很棒,所以我想我必须使用AJAX调用和JavaScript。但是页面重新加载也会对我有很大帮助。

1 个答案:

答案 0 :(得分:0)

使用枚举时,Rails为您创建方法,因此您可以直接调用类别,例如Expense.education

这里是一个如何进行管理的示例,非常原始,假设您使用的是标准Rails约定。不需要特殊的路由。


定义一个包含类别的常量,例如在关注点中:

# model/concerns/expenses_stuff.rb
module ExpensesStuff
  extend ActiveSupport::Concern

  CATEGORIES = {category_a: 0, category_b: 100, category_c: 200 }

  module ClassMethods
   # class methods here
  end

  # instance methods here

end


模型中,将模块包括在Expense类中,并将类别字段定义为枚举,并选择常量:

# models/expense.rb
class Expense < ApplicationRecord
  include ExpensesStuff
  enum category: MaterialStuff::CATEGORIES
  # usual stuff
end


现在在控制器中。仅当包含在MaterialStuff::CATEGORIES.keys中时,请注意接受过滤器值。

# expenses_controller.rb
class ExpensesController < ApplicationController
# usual stuff
  def index
    @expenses = filtered_expenses
  end

 private
  def sanitized_category_filter
    category = params[:category].to_sym if params[:category]
    return category.to_sym if MaterialStuff::CATEGORIES.keys.include? category
    :all
  end

  def filtered_espenses
    Expense.public_send(sanitized_category_filter) # call directly the method
  end

end


最后,在视图中,您可以添加过滤链接。

# views/expenses/index.html.erb

<style>.bg-yellow {background-color:yellow;}</style> <!-- Move to *.css -->
<% def active_filter(category); 'bg-yellow' if category.to_s == params[:category]; end # move to helpers %>

<% (ExpensesStuff::CATEGORIES.keys << :all).each do |category| %>
  <%= link_to category, expenses_path(category: category), class: active_filter(category) %> |
<% end %>

现在,您只需更新CATEGORIES哈希即可添加新类别。您也可以在表单中将其用作选择选项。