Rails filter with button or link_to using Ransack

前端 未结 2 760
灰色年华
灰色年华 2021-01-16 12:18

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 tryi

相关标签:
2条回答
  • 2021-01-16 12:33

    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

    0 讨论(0)
  • 2021-01-16 12:54

    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?

    0 讨论(0)
提交回复
热议问题