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)