1

我有一个部分工作的搜索栏。但是,似乎从我的主页它不会重定向到我的products#index页面。当我搜索产品时,我得到以下网址:http://localhost:3000/?utf8=%E2%9C%93&search=intel但是,如果我将网址更改为如下所示:http://localhost:3000/search?utf8=%E2%9C%93&search=intel这将起作用。我的设置如下:

搜索控制器

class SearchController < ApplicationController
  def index
    @products = Product.all(:conditions => ['title LIKE ?', "%#{params[:search]}%"])
  end
end

ProductsController.rb

def index
    @products = Product.filter(params[:search], [:title])

    respond_to do |format|
      format.html
      format.json { render json: @products }
    end
  end

应用程序.html.erb

 <%= form_tag search_path, :class => 'navbar-form to-the-right', :method => 'get' do %>
            <%= text_field_tag :search, params[:search], :class => 'span2', :placeholder => 'Search' %>
        <% end %>
      </form>

路由.rb

  match '/search' => 'search#index'

我似乎无法确定为什么这不起作用!

4

2 回答 2

1

您的路线不提供 helper search_path。要启用它,请执行以下操作:

match '/search' => 'search#index', as: :search
于 2013-03-07T19:29:35.977 回答
0

根据你的帖子我试试这个:

class ProductsController < ApplicationController

  def index
   @products = Product.filter(params[:title])

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @products }
    end
  end

产品.rb

 def self.filter (search_title)
  return scoped unless search_title.present?
    where(['title LIKE ?', "%#{search_title}%"])
 end

路由.rb

match '/search' => 'products#index'

应用程序.html

<%= form_tag search_path, :class => 'navbar-form to-the-right', :method => 'get' do %>
 <%= text_field_tag :title, params[:title], :class => 'span2', :placeholder => 'Search' %>
<% end %>
于 2013-03-07T21:37:52.513 回答