Order list of movies and save sorting in cookies
You could improve your code by doing the following:


Do not use and/or but &amp;&amp;/|| since what you need here is boolean operators, not flow control operators. Take a look at this article for a great example on how and when to use each operator.
You are constantly doing instance variable assignment at the one-liners conditionals which makes the code difficult to read (conditional ? @i = x ? @i = y). It's not wrong, but you could assign the result of the operation directly to the instance variable.
You could extract some conditional checks to private methods to state in a more clear way what your code does.
I'm not sure for this one, but I'll go for it. I think that you need the redirect when you set the session. You could just pass it to Movie.sort_by_and_rating(...).


Here's a refactored version of your code:



class MoviesController &lt; ApplicationController
  def index
    @movies = Movie.all
    @all_ratings = Movie.all_ratings    
    @sort = params[:sort] || session[:sort]
    @ratings_filter = params_rating? ? params[:ratings].keys : (session[:ratings] || @all_ratings)

    if set_cookies?
      session[:ratings] = @ratings_filter
      session[:sort] = @sort
    end

    @movies = Movie.sort_by_and_rating(@sort, @ratings_filter)
  end

  private

  def params_rating?
    params[:ratings] &amp;&amp; params[:commit] == 'Refresh'
  end

  def set_cookies?
    params[:sort] != session[:sort] || params[:ratings] != session[:ratings]
  end
end