【问题标题】:search with ror用 ror 搜索
【发布时间】:2012-09-20 03:59:52
【问题描述】:

好的,这是我的问题。我用标题做了一个简单的搜索。 它工作,但分页不!

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>

和来自控制器>store_controller: @buildings = Building.paginate :page=&gt;params[:page], :order =&gt;'created_at DESC', :per_page=&gt;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

问题依然存在!错误:#

的未定义方法“分页”

【问题讨论】:

    标签: ruby-on-rails search pagination


    【解决方案1】:

    您要对搜索结果进行分页,不要再对整个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 classmethod 实际上是一个作用域:

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

    【讨论】:

    • NoMethodError in StoreController#index undefined method paginate' for nil:NilClass app/controllers/store_controller.rb:8:in index'
    • 您必须先加载建筑物,我将编辑响应。我只是跳过了它,因为这些建筑物可能来自一个协会或任何有意义的东西。
    • 仍然是同样的错误。我已经更新了帖子看看
    • 您能检查一下您的控制器和模型是否与我在答案中写的相符吗?
    • NoMethodError in StoreController#index undefined method paginate' for #&lt;Array:0x6b20f20&gt; app/controllers/store_controller.rb:12:in index'
    猜你喜欢
    • 2015-07-20
    • 1970-01-01
    • 2023-03-04
    • 2015-02-04
    • 2011-12-01
    • 2011-01-15
    • 2013-12-17
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多