2016-05-11 68 views
1

我可以過濾和搜索時with_role是空白的,但是當我想用with_role搜索搜索:主管或員工我有一個問題:不能過濾和使用filterrific

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

我怎樣才能解決,或轉換該數組?

這裏是我的代碼 控制器

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 

型號

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 } 

回答

0

上午以及新的使用Filterrific寶石。

您可能要檢查您的控制器上的select_option線,你必須

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

檢查什麼[%w(Supervisor supervisor), %w(Técnico worker)]呼喚,這是我無法從你的模型回升。它假設是您的模型中定義的範圍的選項。

需要注意的是:在select_options

您是否正存儲<select>輸入任何選項在filterrific形式在這裏。 #options_for_...方法返回的數組可以作爲選項傳遞給f.select,因此這些方法在模型中定義。

欲瞭解更多信息,我建議你去通過Filterrific Controller Explanation

另一件事我覺得你可以做的是:

定義模型中的方法,例如,你在你的選擇解析[%w(Supervisor supervisor), %w(Técnico worker)]

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

然後在您的控制器中調用方法options_for_with_role select_option suc h上它看起來像這樣:

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 

如果它不工作,當找到一個方法來行User.where(role: %w(supervisor worker), company_id: current_user.company_id)改變你的ActiveRecord的基於模型的課呢。