0
是我的問題。我使用標題進行簡單搜索。 它工作,但paginate不!搜索與ROR
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'爲#
NoMethodError在StoreController#索引 未定義的方法'PAGINATE」的零:NilClass 應用程序/控制器/ store_controller.rb:8:在'索引' – marios
你必須先加載建築物,我會編輯迴應。我只是略過了它,因爲這些建築物可能來自一個協會或任何有意義的東西。 – rewritten
仍然保持相同的錯誤。我已更新帖子看看 – marios