I'm taking a course on Rails, and I need to "remember" the values selected by the user across https requests, in order to filter a list.
I am supposed to do this in the session 'Hash'.
Here is my index:
def index
#list all different values for the key ratings
@all_ratings = Movie.uniq.pluck(:rating)
#order movie list based on interaction with link from view
@movies = Movie.order(params[:sort_param])
#filter movies by rating, based on checkbox from view
@movies = @movies.where(:rating => params[:ratings].keys) if params[:ratings].present?
# I intend to store here the ratings selected, and then save in session
@selected_ratings = params[:ratings].present? ? params[:ratings] : [])
#It gives this error: undefined local variable or method `selected_ratings'
session[:selected_ratings] = @selected_ratings
end
Here my view:
%h1 All Movies
= form_tag movies_path, :method => :get do
Include:
- @all_ratings.each do |rating|
= rating
= check_box_tag "ratings[#{rating}]", @all_ratings, @selected_ratings.include?(rating)
= submit_tag 'Refresh'
%table#movies
%thead
%tr
%th{:class=> helper_class('title'), :id => ('title_header')}= link_to 'Movie Title', movies_path(sort_param: 'title')
%th Rating
%th{:class=> helper_class('release_date'), :id => 'release_date_header'}= link_to 'Release Date',movies_path(sort_param: 'release_date')
%th More Info
%tbody
- @movies.each do |movie|
%tr
%td= movie.title
%td= movie.rating
%td= movie.release_date
%td= link_to "More about #{movie.title}", movie_path(movie)
And now the questions:
When I render the debug in the view, and I choose one filter (PG in this case), I get this:
--- !ruby/hash-with-ivars:ActionController::Parameters elements: PG: G R PG-13 PG ivars: :@permitted: falseI don't quite understand that. I guess
ivarsare instance variables, but I don't know why the hash is not shown in a proper{hash}, and only with the values selected.How do I use
session[:selected_ratings]to remember the filter for the movies? I mean, how do I use session as a parameter.Maybe:
@movies [email protected](:rating => session[selected_ratings].keys)Where can I read about how to use session, how to store, how to access and use the params stored... I have read this, and that, and also some blogposts about sessions, authentication... But I didn't manage to understand and apply to other situations than the ones described in the blog.
session[selected_ratings]bysession[:selected_ratings]