简体   繁体   中英

Rails Filterrific - sorting always reverts to created_at desc

I'm trying to implement Filterrific into my Rails app and I diligently followed the instructions on their website, but my default_filter_params always reverts to model's default of created_at desc .

Here is what I have:

#suggestion.rb

scope :sorted_by, lambda { |sort_option|
  # extract the sort direction from the param value.\
  direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
  case sort_option.to_s
  when /^created_at_/
    order("suggestions.created_at #{ direction }")
  else
    raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
  end
}

filterrific(
    default_filter_params: { sorted_by: "created_at_asc" }, #I changed this to ascending order, but it always sorts in descending order
    available_filters: [
        :sorted_by
    ]
)

#suggestions_controller.rb

def index

@filterrific = initialize_filterrific(
    Suggestion,
    params[:filterrific]
) or return
@suggestions = @filterrific.find.page(params[:page])
respond_to do |format|
    format.html
    format.js
end

... #other code

Thanks for any help.

I just figured out that the code above is correct; it was just a cookie issue. When I cleared my cookies and modified my code, it switches between ascending and descending just fine (I was using Opera as my browser).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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