简体   繁体   中英

Can't filter and search using filterrific

I can filter and search when with_role is blank but when I want to search using with_role: supervisor or worker I have a problem:

NoMethodError (undefined method `with_role' for #Array:0xb2499b54):
app/controllers/workers_controller.rb:38:in index'

How can I fix it or convert that array?

Here's my code Controller

class WorkersController < ApplicationController
  include WorkerHelper
  def index
    (@filterrific = initialize_filterrific(
      User.where(role: %w(supervisor worker), company_id: current_user.company_id),
      params[:filterrific],
      select_options: {
        with_role: [%w(Supervisor supervisor), %w(Técnico worker)],
        search_query: ['Nro. DNI', 'Nombres', 'Apellidos']
      },
      persistence_id: false
    )) || return
    @workers = @filterrific.find.paginate(per_page: 10, page: params[:page])
  end
end

Model

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :validatable, :trackable, :timeoutable

  filterrific(available_filters: [:search_query, :with_role])
  belongs_to :company
  has_one :session
  has_many :service_workers, foreign_key: :worker_id
  has_many :services, foreign_key: :supervisor_id
  has_many :work_services, through: :service_workers, source: :service
  validates :document_number, presence: true, uniqueness: { case_sensitive: false }
  scope :principal, -> { order(role: :desc) }
  before_update :update_password_change_required, if: :encrypted_password_changed?
  before_update :disable, if: :status_changed?
  after_commit lambda {
    if transaction_record_state(:new_record)
      logger.info "CREATE User: #{inspect}"
    elsif !(transaction_record_state(:new_record) || destroyed?)
      logger.info "UPDATE User: #{inspect}"
    end
  }

  scope :search_query, lambda { |params|
    return nil if params.query.empty?
    case params.option
    when 'Nro. DNI'
      select { |w| w.document_number.to_s.include? params.query.downcase }
    when 'Nombres'
      select { |w| w.first_name.downcase.include? params.query.downcase }
    when 'Apellidos'
      select { |w| w.last_name.downcase.include? params.query.downcase }
    end
  }

  scope :with_role, lambda { |role|
    select { |u| u.role == role }
  }

end

Index.html.haml

.card.no-padding
    = form_for_filterrific @filterrific do |f|
      .items-right.search
        %span Filtrar por:
        .custom-select.blue-arrow
          = f.select(:with_role, @filterrific.select_options[:with_role],{ include_blank: 'Todos' })
        %span Buscar por:
        = f.fields_for :search_query do |field|
          .custom-select.blue-arrow
            = field.select :option, @filterrific.select_options[:search_query]
          = field.text_field :query, class: 'filterrific-periodically-observed form-control', id: 'search-text', placeholder: 'Ingrese DNI'
        = link_to 'VER TODO', reset_filterrific_url, class: 'btn btn-blue-reverse'
    = render partial: 'list', locals: { workers: @workers }

Am as well new to using Filterrific Gem.

You might wanna check the select_option line in your Controller where you have

with_role: [%w(Supervisor supervisor), %w(Técnico worker)],

Check what [%w(Supervisor supervisor), %w(Técnico worker)] is calling, which I can't pick up from your Model. Its suppose to be option for your scope defined in your Model.

Note that: In select_options

You are storing any options for <select> inputs in the filterrific form here. The #options_for_... methods return arrays that can be passed as options to f.select , so these methods are defined in the model.

For more information I suggest you go through Filterrific Controller Explanation

Another thing I think you can do is:

Define a method in your model such that you parse in your options [%w(Supervisor supervisor), %w(Técnico worker)]

def self.options_for_with_role
    [
        ['Name (a-z)', 'name_asc'] #Your options goes in here
    ]
end

Then call the method options_for_with_role inside your Controller select_option such that it looks like this:

class WorkersController < ApplicationController
  include WorkerHelper
  def index
    (@filterrific = initialize_filterrific(
      User.where(role: %w(supervisor worker), company_id: current_user.company_id),
      params[:filterrific],
      select_options: {
        with_role: #Your ActiveRecord based model class.options_for_with_role e.g. Worker.options_for_with_role,
        search_query: ['Nro. DNI', 'Nombres', 'Apellidos']
      },
      persistence_id: false
    )) || return
    @workers = @filterrific.find.paginate(per_page: 10, page: params[:page])
  end
end

If it didn't work, when find a way to change the line User.where(role: %w(supervisor worker), company_id: current_user.company_id) to Your ActiveRecord based model class too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM