【问题标题】:Building and handling multiple param queries with Rails 3使用 Rails 3 构建和处理多个参数查询
【发布时间】:2012-09-30 13:36:49
【问题描述】:

我对如何处理 URL 中的多个参数输入以在 Rails 3.2 中构建过滤器有点迷茫

基本上,我想带http://example.com/products/?category=24http://example.com/products/?category=24&country=24一起工作。

我有以下型号ProductCountryCategoryCategorization

我可以在我的ProductController 中建立一个简单的条件

def index
  @products = product.all

  if params[:country]
    @products = Country.find(params[:country]).products
  end

  if params[:category]
    @products = Category.find(params[:category]).products
  end

end

我想知道解决这个问题的最佳方法,因为我的 CategoryProduct 模型通过 Categorization 模型通过 has_many 关联。

class Product < ActiveRecord::Base

  has_many :categorization
  belongs_to :country

end

class Category < ActiveRecord::Base
  has_many :categorization
  has_many :products, through: :categorization

  attr_accessible :name

end

class Categorization < ActiveRecord::Base
  belongs_to :category
  belongs_to :product

  attr_accessible :category, :product
end

我的视图布局模板是一个简单的类别和国家链接列表。因此,如果有人点击一个名为“Toys”的类别并点击国家“England”,它应该建立一个类似这样的链接:http://www.example.com/products/category=12&country_id=1

<div class="wrapper">
  <div class="products">
  </div>
  <div class="sidebar>
   <h2>Categories</h2>
   <ul>
     <% @categories.each do |category| %>
       <li><%= link_to category.name, params.merge(category: category.id) %></li>
     <% end %>
   </ul>
    <h2>Countries</h2>
   <ul>
     <% @countries.each do |country| %>
       <li><%= link_to country.name, params.merge(country: country.id) %></li>
     <% end %>
   </ul>
  </div>
</div>

更新

我已经接受了在模型类上创建过滤器的建议,下面的代码可以工作,但它看起来很乱而且不干..有人可以帮助解决另一种方法。

# Product Model

class Product < ActiveRecord::Base 

  # Search filter
  def self.filter_by_params(params)
    if params.has_key?(:category) && params.has_key?(:country_id)
      scoped.joins(:categorization).where("category_id = ? AND country_id = ?", params[:category], params[:country_id])
    elsif params.has_key?(:category)
      scoped.joins(:categorization).where("category_id = ?", params[:category])
    elsif params.has_key?(:country_id)
      scoped.where(country_id: params[:country_id])
    else
      scoped
    end
  end

end

# Product Controller

def index
  @product = Product.filter_by_params(params)
end

非常感谢您的建议。

WD

【问题讨论】:

  • 我还认为我可以保持构建条件,但这可能不是处理逻辑的好方法。例如。 if params[:category] ​​&& params[:country_id].

标签: ruby-on-rails activerecord ruby-on-rails-3.2


【解决方案1】:

我会将您的ProductsController 更改如下:

def index
  @products = Product.filter_by_params(params)
end

然后在您的Product 模型中:

def self.filter_by_params(params)
  scoped = self.scoped
  scoped = scoped.where(:country_id => params[:country_id]) if params[:country_id]          
  scoped = scoped.where(:category_id => params[:category_id]) if params[:category_id]
  scoped
end

您需要更改视图以传递 category_id 和 country_id。

【讨论】:

  • 谢谢杰森。我会试一试,让你知道。我不知道我可以从模型中检索 params 哈希而不在 Controller 中为其分配一个变量。
  • 你不能,你将参数哈希传递给模型方法。
  • 再次感谢杰森。在分配作用域变量并在最后调用作用域时,我似乎遇到了问题。给出错误:nil:NilClass 的未定义方法“where”。
  • 我更新了答案。作用域方法是Product上的类方法,所以你可以做self.scopedProduct.scoped
  • 非常感谢杰森。我确实对你的建议有疑问。第二个作用域变量(不调用 self)如果 params[:category_id] 给我一个错误 undefined method where' for nil:NilClass or in my case undefined method join' for nil:NilClass 但在第二个作用域调用 self 例如。 scoped = self.joins() 有效。我想我的问题是..我是否必须根据条件构建范围,例如。 scoped = scoped.where(...)if params[:category] ​​&& params[:country_id] && params[:size]。再一次,非常感谢你或与我一起裸露并帮助我。
猜你喜欢
  • 2013-12-14
  • 2016-05-31
  • 1970-01-01
  • 1970-01-01
  • 2018-02-17
  • 2021-11-28
  • 2011-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多