如何使用范围属性rails进行搜索?

时间:2014-12-18 21:27:31

标签: ruby-on-rails ruby

我使用bootstrap-slider,现在我按直径过滤,只向用户显示选定的范围,并且直径参数有问题

我的表格

<%= form_tag @sub_category, method: 'get' do |d| %>
<%= text_field_tag "diameter[]", nil, id: "ex2", :data => {'slider-min' => '1','slider-max' => '1000','slider-step' => '5','slider-value' => '[250,450]' } %>
<%= submit_tag 'Save' %>
<% end %>

控制器动作

def show
        @sub_category = SubCategory.friendly.find(params[:id])
        @items = @sub_category.items
        @items = @sub_category.items.where("size >= ?", params[:diameter].first) if params[:diameter].present?
        @items = @sub_category.items.where("size <= ?", params[:diameter].last) if params[:diameter].present?
        @items = @items
    end

和形式的直径参数

Processing by SubCategoriesController#show as HTML
  Parameters: {"utf8"=>"✓", "diameter"=>["21,451"], "commit"=>"Save"

如何将直径划分为21和451?

1 个答案:

答案 0 :(得分:0)

似乎diameter在数组中返回逗号分隔的字符串。你应该解析它:

def show
  @sub_category = SubCategory.friendly.find(params[:id])
  @items = @sub_category.items
  if params[:diameter].present?   
    min, max = params[:diameter].first.split(',')
    @items = @items.where("size >= ? and size <= ?", min, max)
  end
end