Reputation: 47
i am trying to display a set of movie list. i am suppose to maintain sessions and url parameters for user request of ratings(different type) and sorting order (by title and the rating). but one of the session is not been maintained.
controller:
def index
@all_ratings = Movie.ratings
@sorted_by = (params[:sort_by] || session[:sorted_by] || :none).to_sym
@selected_ratings = params[:ratings] ? params[:ratings].keys : (session[:selected_ratings] || @all_ratings)
session[:sorted_by] = @sorted_by
session[:selected_ratings] = @selected_ratings
@movies = Movie.where(:rating => @selected_ratings).order @sorted_by
end
each time i refresh a request the session[:selected_ratings] is maintained but the session[:sorted_by] is not. i worked out both of them in similar manner.but the order of movie list is never maintained. params[:sorted_by] work each time. please help.
view:
%th {params[:sort_by] == "title" ? {:class => "hilite"} : {}}= link_to "Movie Title", movies_path(:sort_by => "title"), :id => "title_header"
%th Rating
%th{params[:sort_by] == "release_date" ? {:class => "hilite"} : {}}= link_to "Release Date", movies_path(:sort_by => "release_date"), :id => "release_date_header"
%th More Info
Upvotes: 1
Views: 119
Reputation: 4966
First up, in your view you should be referencing the @sorted_by
instance var rather than param[:sort_by]
, as the param might be nil.
Second, I suspect that params[:sort_by]
is getting set to the empty string rather than nil
as you expect. This is causing the or statement to short-circuit before it hits the session variable, and gives you an empty sort string every time.
Things to try: set up a debugger (pry and/or the debugger gem) and add a breakpoint in the index method somewhere. Then you can inspect the values of everything and see what's actually happening.
Upvotes: 1