简体   繁体   English

使用has_scope gem和collection_select筛选索引

[英]Filter index with has_scope gem and collection_select

I've got a two models: Business and Category . 我有两个模型: BusinessCategory In my business#index view, all Business names are displayed in a table, along with their associated Category . 在我的business#index来看,所有的Business名称显示在一个表中,与他们一起相关的Category I'm using the has_scope gem and added two scopes to my Business model: :by_category and search . 我使用has_scope gem并将两个范围添加到我的Business模型中:by_categorysearch The search scope works fine. search范围工作正常。 The by_category scope does nothing when I select a category from the collection_select and submit the form, but if I hard-code it into the URL in my browser, it works fine. 当我从collection_select选择一个类别并提交表单时, by_category范围不会执行任何操作,但是如果我将其硬编码到浏览器中的URL中,它将很好地工作。

What I've noticed is that if I hard code the URL as such http://host/businesses?by_category=14 it works. 我注意到的是,如果我将URL像http://host/businesses?by_category=14这样硬编码,它将起作用。 This is what I see in the logs: 这是我在日志中看到的:

Started GET "/businesses?by_category=14" for 127.0.0.1 at 2015-01-28 11:48:07 -0600
Processing by BusinessesController#index as HTML
Parameters: {"by_category"=>"14"}

But when I submit the form with a category selected, the URL appears as http://host/businesses?utf8=%E2%9C%93&search=&category%5Bid%5D=14 . 但是,当我提交具有选定类别的表单时,URL显示为http://host/businesses?utf8=%E2%9C%93&search=&category%5Bid%5D=14 This is what I see in the logs when the form is submitted this way: 这是我以这种方式提交表单时在日志中看到的内容:

Started GET "/businesses?utf8=%E2%9C%93&search=&category%5Bid%5D=14" for 127.0.0.1 at 2015-01-28 11:45:57 -0600
Processing by BusinessesController#index as HTML
Parameters: {"utf8"=>"✓", "search"=>"", "category"=>{"id"=>"14"}}

There's something wrong with the way my form is passing the by_category param, but I just can't put my finger on it. 什么毛病我的形式传递的方式by_category PARAM,但我不能把我的手指上。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Here's the relevant code. 这是相关的代码。

Models: 楷模:

class Business < ActiveRecord::Base
  belongs_to :category
  ...
  ...
  scope :by_category, -> category { where(:category => category) }
  scope :search, ->(search) { where(arel_table[:title].matches("%#{search}%")) }
end

class Category < ActiveRecord::Base
  has_many :businesses
end

Business Controller: 业务主管:

class BusinessesController < ApplicationController
  has_scope :by_category
  has_scope :search
  ...
  def index
    @businesses = apply_scopes(Business).all.page(params[:page])
    @categories = Category.all
  end   
end

Business Index: 业务指标:

....
<%= simple_form_for businesses_path, :method => 'get' do %>
  <%= text_field_tag :search, '', placeholder: "Search..." %>
  <%= collection_select :category, :id, @categories, :id, :name, :prompt => "Select Category" %>
  <%= submit_tag "Search", :name => nil %>
<% end %>
...

1st: Notice here, you are submitting the form with a method: 'get', this is the reason why your URL is being encoded. 1st:请注意,您正在使用以下方法提交表单:“ get”,这就是对您的URL进行编码的原因。

<%= simple_form_for businesses_path, :method => 'get' do %>
  <!-- other code goes here --> 
<% end %>

Forms should always get submitted with 'post' request not 'get' . 表单应始终通过“ post”请求而不是“ get”提交。

You should make it method: :post , which will give you proper URL params(as you were expecting during inspection). 您应该将其设为method::post ,它将为您提供正确的URL参数(如您在检查期间所期望的那样)。

2nd : 第二:

Let' assume, this form is getting submitted to an action search : 假设此表单正在提交给动作搜索:

def search 
   raise params.inspect 
 end

With above raise statement enabled when you will hit this particular action, then this will list the params(in hash). 启用上述raise语句后,当您执行此特定操作时,这将列出参数(以散列方式)。 Then you can always fetch the attributes as per your need. 然后,您始终可以根据需要获取属性。

Hope that helps :) 希望有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM