好的,这是我的问题。我用标题做了一个简单的搜索。它工作,但分页不!
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
问题依然存在!错误:未定义的方法“分页”#