用ror搜索

时间:2012-09-28 09:18:54

标签: ruby-on-rails search pagination paginate

好的,这是我的问题。我用标题进行简单的搜索。 它的工作原理但是没有分页!

contollers>store_controller

class StoreController < ApplicationController
  def index

  @buildings = Building.search(params[:search])

  @buildings = Building.paginate :page=>params[:page], :order =>'created_at DESC', :per_page=>10

 end

end

模型&GT;建立

  def self.search(search)
  if search

    find(:all, :conditions => ['title LIKE ?', "%#{search}%"])
   else
    find(:all)
  end
end

视图&GT;存储

 <% if notice%>
<p id="notice"> <%= notice%></p>
<%end%>

<h1>All Priorities</h1>

 <%= form_tag  store_path, :method => 'get'  do %>
<p>
  <%=text_field_tag :search , params[:search]%>
  <%= submit_tag "Search", :name=> nil%>
  </p>
  <%end%>


<% @buildings.each do |building| %>
<div class="entry">
    <div class="img">
    <%= image_tag (building.photo.url)%></div>
    <div class=" disc">
    <h3>Name of the Bulding:  <%= building.title %></h3>
     <h4>Status: <%= building.status %></h4>
    Info: <%=sanitize(building.description)%>
    <div class="price_line">
        <span class="price">Price: <%= sprintf("€ %0.02f",building.price)%></span><br/>
        <div class="button">
        <%= button_to("I want to see it", {:controller => "seeits", :action => "new", :building_id => building.id})%></div>


    </div>

    <div class="pages">
<%= will_paginate @buildings %></p>
</div>

    </div>

</div>


<%end%>

控制器&GT;建筑

class BuildingsController < ApplicationController
  # GET /buildings
  # GET /buildings.json
  def index 

    @buildings = Building.all

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

  end

如果我这样,搜索不起作用,并向我显示所有建筑物。但如果我从视图中删除&gt;存储:

<div class="pages">
    <%= will_paginate @buildings %></p>
    </div>

来自控制器&gt; store_controller:  @buildings = Building.paginate :page=>params[:page], :order =>'created_at DESC', :per_page=>10

工作正常!但我希望在用户位于第一面并且不进行搜索时进行分页

更新1

控制器&GT;存储

class StoreController < ApplicationController
   def index


    # load @buildings following authorizations, whatever...
    # for instance, if you use cancan and load_and_authorize_resource, you'll have
    # already a @buildings you'd want to use, otherwise load them
    @buildings = Building.all
    # or @buildings = current_user.buildings or whatever makes sense

    @buildings = @buildings.search(params[:search]) if params[:search].present?

    @buildings = @buildings.paginate(:page=>params[:page],
                                     :order =>'created_at DESC',
                                     :per_page=>10)
    # do things, render etc...
  end

end

模型&GT;建筑

class Building < ActiveRecord::Base
 scope :search, lambda { |text| where('buildings.title LIKE ?', "%#{text}%") }

end

问题仍然存在!错误:#

的未定义方法`paginate'

1 个答案:

答案 0 :(得分:0)

您想要对搜索结果进行分页,不要再对整个Building表进行分页!

class StoreController < ApplicationController
  def index

    # load @buildings following authorizations, whatever...
    # for instance, if you use cancan and load_and_authorize_resource, you'll have
    # already a @buildings you'd want to use, otherwise load them
    # or @buildings = current_user.buildings or whatever makes sense

    @buildings ||= Building.scoped

    @buildings = @buildings.search(params[:search]) if params[:search].present?

    @buildings = @buildings.paginate(:page=>params[:page],
                                     :order =>'created_at DESC',
                                     :per_page=>10)
    # do things, render etc...
  end

end

并且,search类方法实际上是一个范围:

class Building < ActiveRecord::Base
  scope :search, lambda { |text| where('buildings.title LIKE ?', "%#{text}%") }
end