Rails filter with button or link_to using Ransack

2019-02-27 18:54发布

I am developing a rails application using the Ransack gem and below is the code that I have written so far to filter my database which works like a charm. Now what I am trying to do is to add additional button like filter options to my index view (where each button has pre-defined filter value). In other words, once the database is first filtered with a brand name, then I would like users to be able to further filter the database by clicking one of the buttons which has a pre-defined filter value of say 'colour = white', then rails will show all the data with the selected brand name and the colour of white).

Controller

class ProjectsController < ApplicationController

def index
@q = Project.ransack(params[:q])
@projects = @q.result(distinct: true)
@projects_count = @q.result.count
@projects = Kaminari.paginate_array(@projects).page(params[:page]).per(30)
end

Index View

<%= search_form_for @q, remote: true, :builder => SimpleForm::FormBuilder  do |f| %>
<%= f.input :brand_id_eq, label: false, collection: Brand.all.map{ |f| [f.name, f.id] }, prompt: "All", input_html: { class: "form-control" } %>
<%= f.button :submit, label: "Search", input_html: { class: "btn btn-primary" } %>
<% end %>
...
<span class="data-sort all"><%= link_to "All",q: {color_cont: 'white'}, :class => 'link-sort', :remote => true, :method => :post %></span>

index.js.erb

$('#projects').html('<%= escape_javascript (render partial: 'index') %>').hide().fadeIn('slow');

The problem that I am facing with this approach using the Ransack gem is that when I click the link_to filter button, it does filter the database with the pre-defined filter value of 'white color' however it resets all the previously selected filter options.

Is my approach correct or any better way to achieve this other than using the link_to option?

SOLUTION

I finally got this working using the rail's scope method and a simple jQuery code as shown in my final code below. One thing that I did initially wrong was that I set the name of the scope same as one of my db column name which caused an error. Once I changed the scope name to 'status1', not 'stock_no', it started to work. Hope this helps.

Defined Scope

class ApplicationRecord < ActiveRecord::Base

scope :status1, -> { where( stock_no = "15251" ) }

def self.ransackable_scopes(auth_object = nil)
[:status1]
end

Index.erb

<%= f.hidden_field :status1 %>
<%= f.submit "Stock", :id => "status1", :onclick => "document.getElementById('q_status1').value = 1;", class: 'btn btn-primary status1' %>

2条回答
放荡不羁爱自由
2楼-- · 2019-02-27 19:14

Try this question. It is somewhat what you are trying to do, except instead of a submit button, just make yours a button for filtering. It needs to be inside your search_form_for I'm pretty sure as well. And then write a jquery function to submit when the button is clicked like:

$(document).on("turbolinks:load", function(){
    $(".data-sort").on('click', function() {
        $("form.your-search-form-classname").trigger('submit.rails');
    });
});

UPDATE

Try removing the (boolean = true) attribute from the scope. I tested with a similar app of my own and it worked well.

UPDATE 2

I put the following in my app (where status is a column in by db just like your stock_no) and a got the correct query from my database:

<%= f.hidden_field :stock_no %>
<%= f.submit "Stock", :id => "stock_no", :onclick => "document.getElementById('q_stock_no').value = 1;", class: 'btn btn-primary stock_no' %>

scope :stock_no, -> { where( status: 2 ) }

def self.ransackable_scopes(auth_object = nil)
    [:stock_no]
end

Are you sure you are putting the scope in the right model?

查看更多
我命由我不由天
3楼-- · 2019-02-27 19:21

Replace

<%= link_to "All",q: {color_cont: 'white'}, :class => 'link-sort', :remote => true, :method => :post %>

with

<%= link_to "All",q: {color_cont: 'white', brand_id_eq: params[:q][:brand_id_eq]}, :class => 'link-sort', :remote => true, :method => :post %>

Here assumption is

Once the database is first filtered with a brand name, then I would like users to be able to further filter the database by clicking one of the buttons which has a pre-defined filter value

查看更多
登录 后发表回答