0

I am trying to sort an array in the view by using link_to's, however I'm not getting any changes in the results.

The view -

 = link_to 'Rating', sort: "rating"
 = link_to 'Origin', sort: "origin"
 = link_to 'Meal Time', sort: "meal_time"

The controller -

 def index
   @recipes = Recipe.order(params[:sort])
 end
 #####
 def sort
  if (params[:order] == 'rating')
    @recipes.order('rating')
  elsif (params[:order] == 'origin')
    @recipes.order('origin')
  end
  respond_to do |format|
    format.html { redirect_to :index }
    format.json { render json: @recipe }
  end
end

Am I missing something? I was using the ASCIIcast for sortable table columns as it's the most simple solution I've found to this problem. Here's the repo - https://github.com/luchia/eaten.

EDIT: As suggested by wurde, I kept the Turbolinks gem and and changed the code in the view -

=link_to('Rating', sort: 'rating', 'data-no-turbolink' => true)
=link_to('Origin', sort: 'origin', 'data-no-turbolink' => true)
=link_to('Meal Time', sort: 'meal_time', 'data-no-turbolink' => true)

But this results in a weird bug where the attribute no-turbolink is now a physical part of the URL (image attached)

2 Answers2

1

This could be an issue with Rails turbolinks caching, since you are using a link to 'pull' a new index of recipes. Try removing turbolinks to verify.

Here is a Railscasts solution: Sortable Table Columns
Here is a relevant StackOverflow QA: Using turbolinks in a Rails link_to

Community
  • 1
  • 1
wurde
  • 2,487
  • 2
  • 20
  • 39
0

I've still been unable to get this to work and have tried a number of different solutions including different gems. No luck.