Filterrific gem数组rails

时间:2017-11-22 09:14:16

标签: ruby-on-rails arrays ruby rubygems

如何为filterrific初始化数组并过滤视图?

@reserves = Array.new

我的[[]]的未定义方法`filterrific_default_filter_params'出错:Array

控制器

@filterrific = initialize_filterrific(@reserves.split,params[:filterrific],
select_options: {
 sorted_by: Reserve.options_for_sorted_by,
}) or return
 @reserves = @filterrific.find.page(params[:page])
 respond_to do |format|
   format.html
   format.js
 end

模型

filterrific(
    default_filter_params: { sorted_by: 'created_at_desc' },
    available_filters: [
      :sorted_by,
      :search_query
    ]
  )

  scope :search_query, lambda { |query|

  return nil  if query.blank?

  terms = query.downcase.split(/\s+/)

  terms = terms.map { |e|
    (e.gsub('*', '%') + '%').gsub(/%+/, '%')
  }
  num_or_conds = 2
    where(
      terms.map { |term|
        "(LOWER(reserves.first_name) LIKE ? OR LOWER(reserves.last_name) LIKE ?)"
      }.join(' AND '),
      *terms.map { |e| [e] * num_or_conds }.flatten
    )
  }


  scope :sorted_by, lambda { |sort_option|
  direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
  case sort_option.to_s
  when /^created_at_/
    order("reserves.created_at #{ direction }")
  when /^name_/
    order("LOWER(reserves.last_name) #{ direction }, LOWER(reserves.first_name) #{ direction }")
  else
    raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
  end
}

查看

<% @reserves.each do|reserve| %>
  <tr>
  <% display_reserve = reserves.split %>
  <td><%= display_reserve[5] %> <%= display_reserve[1] %> <%= display_reserve[2] %></td>
<%end%>

当我尝试添加filterrific时,我只使用@reserves = Array.new它会产生错误

0 个答案:

没有答案