简体   繁体   中英

search with ror

ok here is my problem. i make a simple search with title. it work but with paginate dont!

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

model>building

  def self.search(search)
  if search

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

views>store

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

controllers>building

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

if i have it this way the search does not work and shows me all the buildings. but if i remove from views>store :

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

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

it work fine! but i want to paginate when the user is on first side and doesn't do search

UPDATE 1

controllers>store

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

models>building

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

end

the problem remains! error: undefined method `paginate' for #

You want to paginat the results of the search, don't paginate again the whole Building table!

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

and, that search classmethod is in fact a scope:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM