Rails filter with button or link_to using Ransack

左心房为你撑大大i 提交于 2019-12-02 04:03:11

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?

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!