Rails filter with button or link_to using Ransack

前端 未结 2 789
灰色年华
灰色年华 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: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?

提交回复
热议问题