我的搜索栏部分正常运行。但是,从我的主页看来,它似乎没有重定向到我的products#index
页面。当我搜索产品时,我会收到以下网址:http://localhost:3000/?utf8=%E2%9C%93&search=intel
但是,如果我将网址更改为如下所示:http://localhost:3000/search?utf8=%E2%9C%93&search=intel
这将有效。我的设置如下:
SearchController
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
application.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>
的routes.rb
match '/search' => 'search#index'
我似乎无法确定为什么这不起作用!
答案 0 :(得分:1)
您的路线未提供帮助search_path
。要启用它,请执行以下操作:
match '/search' => 'search#index', as: :search
答案 1 :(得分: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
product.rb
def self.filter (search_title)
return scoped unless search_title.present?
where(['title LIKE ?', "%#{search_title}%"])
end
route.rb
match '/search' => 'products#index'
application.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 %>