在 rails 上将地址搜索功能更改为名称 ruby

change address search function to a name ruby on rails

我是 rails 上 ruby 的新手,我目前有一个代码可以搜索地址,我需要更改它。我不需要搜索地址,而是需要搜索一个名为 item_name 的字段。

home.html.erb

<%= form_tag search_path, method: :get do %>
  <div class="row">
    <div class="col-md-6">
      <%= text_field_tag :search, params[:search], placeholder: "What you looking for?", class: "form-control", id: "autolocation" %>
    </div>
 </div> 

pages_controller.rb

class PagesController < ApplicationController

  def home
    @items = Item.where(active: true).limit(3)
  end

  def search

    # STEP 1
    if params[:search].present? && params[:search].strip != ""
      session[:loc_search] = params[:search]
    end

    # STEP 2
    if session[:loc_search] && session[:loc_search] != ""
      @items_address = Item.where(active: true).near(session[:loc_search], 100, order: 'distance')
    else
      @items_address = Item.where(active: true).all
    end

    # STEP 3
    @search = @items_address.ransack(params[:q])
    @items = @search.result
    @arrItems = @items.to_a

  end

  end

end

我通过这样做来工作

  if session[:loc_search] && session[:loc_search] != ""
     @items_names = Item.where('item_name LIKE ?', "%#{params[:search]}%")
  else
    @items_names = Item.where(active: true).all
  end

  @search = @items_names.ransack(params[:q])
  @items = @search.result

  @arrItems = @items.to_a