使用Ransack通过按钮或link_to进行Rails过滤 [英] Rails filter with button or link_to using Ransack

查看:85
本文介绍了使用Ransack通过按钮或link_to进行Rails过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ransack gem开发一个Rails应用程序,下面是到目前为止我编写的用于过滤数据库的代码,它的工作原理就像一个魅力.现在,我想做的是在索引视图中添加其他按钮,例如过滤器选项(每个按钮都具有预定义的过滤器值).换句话说,一旦首先使用品牌名称过滤了数据库,然后我希望用户能够通过单击按钮中一个具有色=白色"的预定义过滤值的按钮来进一步过滤数据库,然后rails将显示具有所选品牌名称和白色的所有数据.

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).

控制器

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

索引视图

<%= 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');

使用Ransack gem的这种方法所面临的问题是,当我单击link_to过滤器按钮时,它会使用白色"的预定义过滤器值来过滤数据库,但是会重置所有先前选择的值过滤器选项.

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.

除了使用link_to选项之外,我的方法是否正确或者是实现此目的的更好方法?

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

解决方案

我最终使用rail的scope方法和一个简单的jQuery代码(如下面的我的最终代码所示)完成了这项工作.我最初做错的一件事是,我将作用域的名称设置为与导致错误的数据库列名称之一相同.一旦将作用域名称更改为"status1"而不是"stock_no",它便开始工作.希望这会有所帮助.

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.

定义范围

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' %>

推荐答案

尝试以下 search_form_for 内部.然后编写一个jquery函数以在单击按钮时提交,例如:

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');
    });
});

更新

尝试从范围中删除(boolean = true)属性.我使用自己的类似应用程序进行了测试,并且运行良好.

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

更新2

我在我的应用程序中放置了以下内容(状态是db的一列,就像您的stock_no一样),并且从我的数据库中获得了正确的查询:

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?

这篇关于使用Ransack通过按钮或link_to进行Rails过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆