我遵循http://filterrific.clearcove.ca中的指南。我尝试添加到我的应用filterrifc gem进行排序,但是它不起作用。我不明白为什么。我将不胜感激。 我的模特:
filterrific(
default_filter_params: { sorted_by: 'created_at_desc' },
available_filters: [
:sorted_by,
:with_title,
:with_spec,
:with_created_at_gte
]
)
belongs_to :user
scope :sorted_by, lambda { |sort_option|
direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
case sort_option.to_s
when /^created_at_/
order("post.created_at #{ direction }")
when /^title_/
order("LOWER(post.title) #{ direction }")
when /^spec/
order("LOWER(post.spec) #{ direction }")
else
raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
end
}
scope :with_title, lambda { |titles|
where(:title => [*titles])
}
scope :with_spec, lambda { |specs|
where(:spec => [*specs])
}
scope :with_created_at, lambda { |ref_date|
where('post.created_at => ?', Data.strptime(ref_date, "%m/%d/%Y"))
}
def self.options_for_sorted_by
[
['title (a-z)', 'title_asc'],
['title (z-a)', 'title_desc'],
['Created date (newest first)', 'created_at_desc'],
['Created date (oldest first)', 'created_at_asc'],
['spec (0-9)', 'spec_asc'],
['spec (9-0)', 'spec_desc'],
]
我的控制器 def秀 @user = User.find(params [:id])
@filterrific = initialize_filterrific(
@user.posts,
params[:filterrific],
select_options: {
sorted_by: @user.posts.options_for_sorted_by },
persistence_id: 'shared_key',
default_filter_params: {},
available_filters: [:sorted_by, :with_title, :with_spec, :with_created_at],
sanitize_params: false
) or return
@posts = @filterrific.find.page(params[:page])
# Respond to html for initial page load and to js for AJAX filter updates.
respond_to do |format|
format.html
format.js
end
rescue ActiveRecord::RecordNotFound => e
puts "Had to reset filterrific params: #{ e.message }"
redirect_to(reset_filterrific_url(format: :html)) and return
end
And my view table:
<div id="filterrific_results">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><%= filterrific_sorting_link(@filterrific, :title) %></th>
<th><%= filterrific_sorting_link(@filterrific, :spec) %></th>
<th>Tags</th>
<th><%= filterrific_sorting_link(@filterrific, :created_at) %></th>
</tr>
</thead>
<tbody id="myTable">
<% @user.posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.spec %></td>
<td><% post.tag_list.each do |tag| %>
<a class="badge badge-info"><%= tag %></a>
<% end %>
</td>
...
</tbody>
</table>
</div>
在终端上一切正常,没有任何错误或异常