2012-09-28 4 views
0

여기 내 문제입니다. 나는 제목으로 간단한 검색을한다. 작동하지만 페이지가 없습니다. 나는 그것을이 검색이 작동하지 않는 방법 및 프로그램이있는 경우ror로 검색

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 

를 구축

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

컨트롤러> 나 모든 건물. 내가보기> 저장소에서 제거하는 경우 만 :

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

및 contollers> store_controller에서

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

그것은 잘 작동! 하지만 난

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

end 

문제를 구축> 사용자> 저장

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 

모델 일측에 있으며

UPDATE 1

컨트롤러를 검색하지 않을 경우 페이지를 매기 할 유적! 오류 : 정의되지 않은 메서드 '페이지 매김'for #

답변

0

검색 결과를 페이지 매기기를 원할 경우 Building 테이블 전체를 다시 매리지 마십시오! search classmethod이 범위 사실에 있음을

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 

과 : StoreController 번호 지수

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

NoMethodError은 정의되지 않은 메서드 전무에 대한'PAGINATE 'NilClass 응용 프로그램/컨트롤러/store_controller.rb : 8 : 'index '에 – marios

+0

건물을로드해야하기 때문에 응답을 수정합니다. 건물이 협회 나 다른 의미에서 생길 수 있기 때문에 방금 건너 뛰었습니다. – rewritten

+0

여전히 같은 오류가 남아 있습니다. 내가 게시물을 업데이 트했습니다 봐 – marios