我使用相同的方法进行分页并在rails中搜索。我正在与product.rb斗争(不知道如何在一种方法中使用两种方法)

时间:2013-06-11 07:17:38

标签: ruby-on-rails

product.rb

分页方法:

def self.search(search, page)  
  paginate :per_page => 5, :page => page,
           :conditions => ['name like ?', "%#{search}%"],
           :order => 'id'
end

以下是搜索方法:

def self.search(search) 
  if search
    find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
  else
    find(:all)
  end
end

product_controller

@products = Product.search(params[:search],params[:page])      

index.html.erb

<%= form_tag products_path, :method => 'get' do %>  
  <p>  
    <%= text_field_tag :search, nil, :placeholder => "Search items here" %>  
    <%= submit_tag "Search", :name => nil %>  
  </p>  
<% end %>

<%= will_paginate @products%>

0 个答案:

没有答案