undefined method `[]' for nil:NilClass on select_options [Filterrific Gem]

750 views Asked by At

Hi I am using Filterrific Gem to add searching and filtering functionalities for my users page.

The Searching works just fine, but I got undefined method[]' for nil:NilClass` error when I tried to implement the "sorted by" filtering.

I refer the Filterrific documentation here: http://filterrific.clearcove.ca/pages/action_view_api.html

Here are my codes:

index.html.erb

<div class="well">
    <%= form_for_filterrific @filterrific do |f| %>
      <div>
        <%= f.text_field( :search_query, id: "filterrific-no-ajax-auto-submit" ,class: 'form-control', placeholder: "Search users...") %>
      </div>

        <div>
            Sorted by
            <% f.select(:sorted_by, @filterrific.select_options[:sorted_by]) %>
        </div>
    <% end %>
</div>

<div class="row">
    <%= render(
      partial: 'users/list',
      locals: { users: @users }
    ) %>
</div>

model.rb

filterrific(
    default_filter_params: { sorted_by: 'first_name_asc' },
    available_filters: [
      :sorted_by,
      :search_query,
      :with_created_at
    ]
)

scope :sorted_by, lambda { |sort_option|
  direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
  case sort_option.to_s
  when /^first_name/

    order("users.first_name #{ direction }")
  else
    raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
  end
}

users_controller.rb

def index
  @filterrific = initialize_filterrific( User, params[:filterrific] ) or return
  @users = @filterrific.find.page(params[:page]).order('first_name ASC')

  respond_to do |format|
    format.html
    format.js
  end
end

Here is the screenshot of the error:

enter image description here

Please help. Thanks!

2

There are 2 answers

0
Shikhar Mann On

While initialization in your controller. The params doesnt contain select_options. They shall contain select_options. Right now it looks as:

"filterrific"=>{"search_query"=>""}

it shall be something like

"filterrific"=>{"search_query"=>"", "select_options"=>YOUR-OPTIONS}

0
Rannyeri Rodrigues On

In your users controller

You need to setup your controller to accept your select options and the other params you will need, just as the with_created_at.

def index
  @filterrific = initialize_filterrific(
    User,
    params[:filterrific],

    select_options: {
      :sorted_by => User.options_for_sorted_by
    }
  ) or return
  @users = @filterrific.find.page(params[:page])

  respond_to do |format|
    format.html
    format.json
  end
end

In your user model

You also gonna need this methods you called in your controller to be written in your model, and you don't need to specify with_created_at like a specific filter, it can be in the options for sorted by just like this example above.

scope :sorted_by, lambda { |sort_option|
  direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'

  case sort_option.to_s
  when /^first_name/
    order("users.first_name #{ direction }")
  when /^last_name/
    order("users.last_name #{ direction }")
  when /^created_at/
    order("LOWER(users.created_at) #{ direction }")
  else
    raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
  end
}

def self.options_for_sorted_by
  [
    ['First Name (a-z)', 'first_name_asc'],
    ['Last Name (a-z)', 'last_name_asc'],
    ['Created at (newer first)', 'created_at_desc'],
    ['Created at (older first)', 'created_at_asc'],
  ]
end

For last but not less important, you must set scopes in order to your search_query works as it needs to. You can define scopes for specific filters like this.

filterrific(
    default_filter_params: { sorted_by: 'first_name_asc' },
    available_filters: [
      :sorted_by,
      :search_query, # defining a search query to find in the db fields
      :school # defining a scope to a foreign key
    ]
)

# here i setup the search query
scope :search_query, lambda { |query|
  where("first_name LIKE ? OR last_name LIKE ?", "%#{query}%", "%#{query}%")
}

# here i setup the scope for a foreign key
scope :school, -> school_id { where(:school_id => school_id) }

To find out more about this gem, here is the link to the documentation. You have to follow all the steps in the sidebar in the order it is displayed to get a more complete and easier understanding of the gem

http://filterrific.clearcove.ca/