Ruby on rails 导航栏中的搜索表单不';我什么也不做
我的导航栏里有一张搜索表Ruby on rails 导航栏中的搜索表单不';我什么也不做,ruby-on-rails,Ruby On Rails,我的导航栏里有一张搜索表 <%= simple_form_for :query, url: clients_products_path, method: :get, wrapper: :inline_form, html: {class: 'form-inline'} do |f| %> <%= f.input :keyword, placeholder: "Recherche" %> <%= f.submit "Valider" %>
<%= simple_form_for :query, url: clients_products_path, method: :get, wrapper: :inline_form, html: {class: 'form-inline'} do |f| %>
<%= f.input :keyword, placeholder: "Recherche" %>
<%= f.submit "Valider" %>
<% end %>
我认为你的问题在于:
@products = Product.where('lower(title) LIKE ?', params[:query][:keyword])
您需要使用%
预先结束、追加或包装查询。例如:
@products = Product.where('lower(title) LIKE ?', "%#{params[:query][:keyword]}%")
# Think it's the above, though could be the following:
# @products = Product.where('lower(title) LIKE "%?%"', params[:query][:keyword])
如果您对SQL的类似
操作符有一个读取,那么%
操作类似于通配符。如果没有这些,您将搜索完全匹配的内容,而不是标题中包含的短语
试一试,让我知道你进展如何。首先,你要检查两次
params[:query]
(一次是在调用filter\u products时,第二次是在该函数中)
而且他们的产品是你的产品功能有问题
当您执行@products | |=Product.all
时,如果查询返回空关系,则ActiveRecordRelation
将为空。换句话说,如果查询[:关键字]
与标题不匹配,则@products
将始终为空
尝试将索引函数更改为:
def index
@products = Product.where('lower(title) LIKE %?%', params[:query][:keyword].downcase) if params[:query][:keyword].present?
puts @products
@products ||= Product.all
end
如果仍然返回空白,则尝试打印@products
变量。请共享您试图显示值的索引
页面的代码。当您没有查询参数时,在索引页面上会得到什么?当没有查询参数时,我会得到所有产品,而我通过查询http://localhost:3000/clients/products?utf8=%E2%9C%93&query%5Bkeyword%5D=jean&commit=Valider
你的意思是什么?不客气,很好,很高兴听到它起作用:)谢谢你的建议,是的,你是对的,我检查了两次,但这最终不是问题:)谢谢
@products = Product.where('lower(title) LIKE ?', params[:query][:keyword])
@products = Product.where('lower(title) LIKE ?', "%#{params[:query][:keyword]}%")
# Think it's the above, though could be the following:
# @products = Product.where('lower(title) LIKE "%?%"', params[:query][:keyword])
def index
@products = Product.where('lower(title) LIKE %?%', params[:query][:keyword].downcase) if params[:query][:keyword].present?
puts @products
@products ||= Product.all
end