我正在尝试将参数传递给我的Product控制器,但它产生错误。
如果我有以下代码:
def index
@products = Product.paginate(:page => params[:page], :per_page => 10)
respond_to do |format|
format.html # index.html.erb
format.json { render json: @products }
end
一切正常。但当我将其修改为:
def index
@products = Product.where('model LIKE ?', '%deluxe%').paginate(:page => params[:page], :per_page => 10) if params[:selected_term] != nil
respond_to do |format|
format.html # index.html.erb
format.json { render json: @products }
end
end
我收到错误消息:
The @products variable appears to be empty. Did you forget to pass the collection object for will_paginate?
在我的views / application.html.erb中,我有:
<% filters = ProductsFilter.all %>
<li><%= link_to "Products", "/product" %></li>
<% if controller_name == 'products' %>
<div class="btn-group">
<button class="btn">Viewpoint</button>
<button class="btn dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<% filters.each do |filter| %>
<li><%= link_to filter.term, tweet_path(:selected_term => filter.term) %></li>
<% end %>
<% end %>
这解决了问题(可能不是最优雅的解决方案):
def index
@products = Product.paginate(:page => params[:page], :per_page => 10)
if (params[:selected_term]) != nil
selected_term = params[:selected_term]
@products = Product.where('Product_text LIKE ?', "%#{selected_term}%").paginate(:page => params[:page], :per_page => 10)
end