mardi 27 septembre 2016

view is not updating after applying filter [ruby rails filterrific]

I have a simple table which displays all the users and its attributes id,name,age,status,location. I want to filter on age and status.

Id name age status location
1  xz    22  single  ca
2  yy    23  married ma

I am using filterrific plugin and able to display the list and the filter dropdown.

My user.rb

 filterrific(
    available_filters: [
      :with_status,
      :with_age
    ]
  )

  scope :with_age, lambda { |age|
    where(age: [*age])
  }

  scope :with_status, lambda { |status|
    where(status: [*status])
  }


  def self.options_for_select
    order('LOWER(status)').map { |e| [e.status] }.uniq
  end
  def self.options_for_select2
    order('LOWER(age)').map { |e| [e.age] }.uniq
  end

The controller index looks like

def index
    @filterrific = initialize_filterrific(
      User,
      params[:filterrific],
      select_options: {
        with_status: User.options_for_select,
        with_clearance_batch_id: User.options_for_select2
      },
      default_filter_params: {},
      available_filters: [],
    ) or return

    @users = @filterrific.find
    respond_to do |format|
      format.html
      format.js
    end

  end

the index.html.erb looks like

<%= form_for_filterrific @filterrific do |f| %>

  <div>
    age
    <%= f.select(
      :with_age,
      @filterrific.select_options[:with_age],
      { include_blank: '- Any -' },
      {class: 'form-control' }
    ) %>
  </div>
  <div>
    status
    <%= f.select(
      :with_status,
      @filterrific.select_options[:with_status],
      { include_blank: '- Any -' },
    ) %>
  </div>

<% end %>

<%= render(
  partial: 'browse_users/list',
  locals: { users: @users }
) %>

When I go to the page I am able to see all the users. Now when I filter nothing happens. I still see all the users. Not sure what is happening. I have feeling that my scope filter is not getting applied.

Aucun commentaire:

Enregistrer un commentaire