搜索Ruby on Rails的两个参数



我正在使用两个文本字段进行搜索。它搜索两个内容(现在的位置和描述),并且只显示与这两个内容匹配的条目(列表)。我希望第二个文本字段也搜索标题,所以它应该查找描述和标题。这是怎么回事?

这就是我现在拥有的

listing.rb

def self.locsearch(search_location, search_description)
  return scoped unless search_location.present? || search_description.present?
  where(['location LIKE? AND description LIKE?', "%#{search_location}%", "%#{search_description}%"])
end

home.html.erb

<%= form_tag findjobs_path, :controller => 'listings', :action => 'locsearch', method: :get do %>
  <%= text_field_tag :location, params[:location] %>
  <%= text_field_tag :descripiton, params[:descripiton] %>
  <%= submit_tag "Search", name: nil %>
  <% end %

listings_controller.rb

def index
@listings = @listings.locsearch(params[:location], params[:description])
end

另外,我的locsearch方法现在使用或||条件。我将如何实现"one_answers"条件?(如果我将||更改为&&我会得到错误"undefined method or variable scroped")

您确定要使用unless

unless仅在条件为false时希望执行指定代码时使用。

并且CCD_ 3与模型一起使用。

Model.scoped

您可以参考Apidock或Github

只有将scoped定义为局部变量或方法时,才能编写return scoped

您还可以查看数据库的范围

最新更新