0

ここに私の問題があります。タイトルで簡単に検索します。それは動作しますが、ページネーションを使用しないでください!

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

モデル>建物

  def self.search(search)
  if search

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

ビュー>ストア

 <% 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%>

コントローラ > 建物

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

このようにすると、検索が機能せず、すべての建物が表示されます。しかし、ビュー>ストアから削除すると:

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

そして contollers>store_controller から: @buildings = Building.paginate :page=>params[:page], :order =>'created_at DESC', :per_page=>10

それはうまくいきます!しかし、ユーザーが最初の側にいて検索を行わないときにページネーションしたい

更新 1

コントローラー>ストア

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

モデル>建物

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

end

問題は残ります!エラー: 未定義のメソッド「paginate」 #

4

1 に答える 1

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
于 2012-09-28T09:28:35.087 に答える