2014-04-29 5 views
0

올바르게 페이지 설정을 시도했지만 자습서를 따르고 제대로 작동하도록하는 데 문제가 있습니다.will_paginate를 사용하여 페이지 매김을 올바르게 설정하십시오.

기본적으로 설치를 원하는 것은 사용자가 페이지에 표시 할 회사의 수를 선택하면 각 페이지의 주어진 회사 수에 페이지 번호를 매기는 다음 루프를 작성하는 것입니다. 사용자가 특정 회사/페이지 수를 선택하지 않으면 기본값으로 설정됩니다.

누구나 내게 올바른 방향으로 밀어 넣을 수 있다면, 그 점에 크게 감사 할 것입니다.

보기

<ul class="list-group"> 
<%= @comps.find_each do |comp| %> 
<li class="list-group-item"> 
<div class="row"> 
    <div class="span12"> 
     <p style="font-size: 1.5em"><strong><%= link_to comp.name, comp %></strong></p> 
     <hr class="divider"> 
     <div class="row"> 
      <div class="span6"> 
       <p><strong>Primary Field:</strong> <%= comp.primary_field %></p> 
       <p><strong>Description:</strong> <%= truncate(comp.description, length: 150) { link_to "Read More", comp, :target => "_blank"}%> 
       </p> 
      </div> 
      <div class="span5"> 
       <% if signed_in? %> 
        <p><% if !(current_user.favorites.include? comp) %> 
          <%= link_to 'Favorite', 
          favorite_company_path(comp, type: "favorite"), 
          class: "btn btn-primary", 
          method: :put%> 

        <% else %> 
          <%= link_to 'Unfavorite', 
          favorite_company_path(comp, type: "unfavorite"), 
          class: "btn btn-danger", 
          method: :put%> 
        <% end %></p> 
       <% end %> 
       <p><strong>Website:</strong> <%= auto_link(comp.website, :html => { :target => '_blank' })%></p> 
       <p><strong>Location:</strong> <%= comp.address %></p> 
      </div> 
     </div> 
    </div> 
</div> 
</li> 
<% end %> 
<%= will_paginate @comps%> 
</ul> 

(목록 페이지가 처리됩니다) 컨트롤러 목록 페이지

class DynamicPages < ActiveRecord::Base 

    self.per_page = 5 

end 
+0

대신 컨트롤러에서이 코드를 사용하면 어떻게 되나요? % => params [: page]> : per_page ' 다음은'%% will_paginate @comps %> '를 사용하는 것입니다. 그리고 모델에서는'self.per_page = 10'을 설정하여'params [: count]'가 선택되지 않았습니다 – robzdc

+0

저는이 문제를 해결하는 데 정말로 가깝지만 문제가 하나 더 있습니다. 아래 답변에 대한 의견을 참조하십시오. 나는 또한 내 코드를 조금 더 업데이트했다. – Scalahansolo

답변

0
에 대한

def filter 
    if ((params[:field] != "") and (params[:field] != "All Fields") and (params[:field] != nil)) 
     # This will apply the filter 
     @comps = Company.where(primary_field: params[:field]) 
    else 
     # This will return all records 
     @comps = Company.all 
    end 
    end 

    def list 
    @fields = @@primary_field 
    filter 
    @comps = @comps.paginate(:page => params[:page], :per_page => params[:number_of_companies] ||= 5) 
    end 

모델

will_paginate를 사용하고 있으므로. 당신은 이런 식으로 할 수 있습니다. 여기

Post.paginate(:page => params[:page], :per_page => params[:number_of_companies] ||= 10) 

PARAMS [: number_of_companies]는 사용자가 선택한 회사 수 있으며 10

기본 페이지 값이다.

클래스 포스트

self.per_page = 10 

또한, 귀하의 코드 <% = @의 comps.find_each 할

| 완 | %>는 < % = %>

+0

나는이 일을하는 것이 정말로 가깝다고 느낍니다. 현재 나는 회사의 수를 기반으로 올바른 페이지 수를 얻고 있지만, 모든 회사가 첫 페이지에 나타나고있는 문제가 여전히 남아 있습니다. 내 루프가 각 페이지에 정확한 수의 회사를 배치하도록 어떻게 설정합니까? – Scalahansolo

+0

변경 했습니까? <% = @ comps.find_each do | comp | %>'~'<% @ comps.find_each do | comp | %>'이미? – robzdc

관련 문제