2

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: false
    

    I don't quite understand that. I guess ivars are 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.

1
  • you need to replace session[selected_ratings]by session[:selected_ratings] Commented Jul 4, 2016 at 22:06

2 Answers 2

1

Why do I get the undefined variable error if selected_ratings is defined?

@selected_ratings is defined; selected_ratings is not. What you meant to do, I think, is something like:

@selected_ratings = (params[:ratings] || session[:selected_ratings] || []) 
session[:selected_ratings] = @selected_ratings

Note that this is using a symbol, rather than an undefined variable.

Let's pretend session[selected_ratings] works, how do I use it to filter the movies? Maybe: @movies [email protected](:rating => session[selected_ratings].keys)

Well, what does @selected_ratings equal? I suspect it's probably an array of ratings, right? - Something like: ["PG", "PG-13", "R"]. In which case, ActiveRecord is clever enough and generating SQL, that you can just do something like:

@movies.where(rating: @selected_ratings)

Most importantly, 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 blogpost about sessions, authentication... But I didn't manage to understand and apply to other situation than the described in the blog.

That's a bit vague; I don't know what to suggest beyond the Rails documentation etc. for a beginners' overview. If you have a more specific question, I'd be happy to help.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, I've been tinkering with the code for a while, and at some point I deleted the colon. I will change the code, and rephrase some questions.
0

Undefined error is because you refer to variable selected_ratings which isn't defined (you defined @selected_ratings, which isn't the same thing).

To answer your second question, you can do like so:

@movies = @movies.where(:rating => session[:selected_ratings])

session hash is easy to work with. You save and retrieve values just like you would with a regular hash.

2 Comments

Thanks, I read somewhere that session is not entirely a hash, although responds like one. And when had
And when have @movies = @movies.where(:rating => session[:selected_ratings]) It does not filter at all. I think it has something to do with the session, is not storing the values properly. The debug in view says --- !ruby/hash-with-ivars:ActionController::Parameters elements: PG-13: G R PG-13 PG PG: G R PG-13 PG ivars: :@permitted: false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.