2010-11-24 17 views
0

Ich versuche, eine contacts paginiert teilweise innerhalb einer Ansicht für rooftops setzen. Rooftops hat viele Kontakte und hat auch eine verschachtelte Route, die /rooftops/1/contacts erlaubt. Wenn ich versuche, die will_paginate Links zu erstellen, bekomme ich /rooftops/1 als Pfad statt "/ Dächer/1/Kontakte /".Rails Paginierung verschachtelte Routen

Gibt es eine Möglichkeit, den Paginierungspfad zu ändern, ohne eine eigene Paginierungslinkansicht erstellen zu müssen? Ich habe versucht, zum rdoc für will_paginate here zu gehen, aber es geht zu einer godaddy Seite.

Irgendwelche Ideen?

class Contact < ActiveRecord::Base 
    has_and_belongs_to_many :parents 
    has_and_belongs_to_many :rooftops 
    has_one :user 
    has_one :address, :as => :addressable, :dependent => :destroy 
    has_many :phone, :as => :phoneable, :dependent => :destroy 
    belongs_to :position 
    accepts_nested_attributes_for :address, :reject_if => lambda { |a| a[:street].blank? } 
    accepts_nested_attributes_for :phone, :reject_if => lambda { |a| a[:phone_number].blank? }, :allow_destroy => true 

    validates_associated :address, :phone, :position 
    validates_presence_of :lname 
    validates_presence_of :fname 
    validates_presence_of :email 
    validates_presence_of :position_id 

    def self.search(page) 
    paginate :per_page => 3, 
       :page => page 
    end 
end 

class RooftopsController < ApplicationController 
    def show 
    @rooftop = Rooftop.find(params[:id]) 
    @contacts = @rooftop.contacts.search(1) 
    end 
end 

<div class='pagination'> 
    <%= will_paginate contacts %> 
</div> 

Das schafft /rooftops/1 und ich bin nicht sicher, wie es /rooftops/1/contacts zu erzwingen.

--------------------- BEARBEITEN ---------------------

Meine Routen sind

resources :rooftops do 
    resources :contacts 
end 

Ich denke, ich weiß, wo mein Problem ist. Da meine Suchfunktion innerhalb des Modells ist. Ich berühre nie wirklich den contacts Controller. Ich mache die Suche von rooftops Controller und rufen Sie eine teilweise aus der contacts Ansicht. Ich werde zeigen, was meine Ansichten sind.

<div id='contacts' style='margin-top: 20px; border-bottom: 1px solid lightgrey;'> 
    <h4>Contacts <%= link_to "new", new_polymorphic_path([@rooftop, Contact]) %></h4> 

<%= render :partial => 'contacts/contacts', :locals => { :object_type => @rooftop, :layer => 1 } %> 
</div> 

Dies ist die tatsächliche teilweise. Wie Sie sehen können, geht es nie wirklich durch den Contacts-Controller. Könnte das die Ursache meines Problems sein?

<table> 
<tr> 
    <th></th> 
</tr> 
<% @contacts.each do |contact| %> 
<tr> 
    <td class='contact_mailer'> 
    <%= link_to image_tag('icons/gray_light/mail_12x9.png'), "mailto:#{contact.email}" %> 
    </td> 
    <td> 
    <div class='expandable layer_<%= layer %>' style='float: left'> 
    <%= link_to contact.fname.titleize + ' ' + contact.lname.titleize, polymorphic_path([object_type, @contact]), :class => "contact_name" %> 
     </div> 
    </td> 
    <td> 
     <%= image_tag 'icons/blue/key_stroke_8x8.png' %> 
    </td> 
</tr> 
<% end %> 
</table> 

<br /> 
<div class='pagination'> 
    <%= will_paginate @contacts %> 
</div> 

Danke für Ihre Hilfe.

+0

die github wiki-Seite von mislav/will_pageinate und die Dokumentation ist wahrscheinlich das gleiche, versuchen Sie es hier: https://github.com/mislav/will_paginate/wiki – fifigyuri

Antwort

1

Es gibt mehrere Orte, an denen Sie falsch liegen könnten. Vielleicht haben Sie Ihre Routen nicht richtig eingestellt. Der will_paginate Anruf sollte in der Kontaktansicht sein, ist es da? Es ist nicht klar aus dem Ausschnitt, den Sie gegeben haben. Die self.search Methode im Contact Modell ist sehr neugierig auf mich, das sollte eher im Controller liegen. Es macht Sinn, die Suchmethode bezieht sich auf die Kontrolle der Contact. Und habe es als Instanzmethode.

Die Route rooftops/:id/contacts sollte zum Contact-Controller führen. Sie könnten prüfen, ob es so ist.

Ich nehme an, Sie so etwas wie dieses brauchen:

class Contact < ActiveRecord::Base 
    # your model definition, validations 
    # no self.search method here 
    cattr_reader :per_page 
    @@per_page = 3 
end 

class RooftopsController < ApplicationController 
    # your RooftopsController methods 
    # the show you listed should be in ContactsController 
end 

class ContactsController < ApplicationController 
    def show 
    # I guess you have contacts for Rooftop 
    @contacts = Rooftop.find(params[:id]).contacts 
    # you paginate the array of results 
    @contacts.paginate(params[:page]) 
    end 

## This is in the view of the Contacts#show 
<div class='pagination'> 
    <%= will_paginate contacts %> 
</div> 

Sie müssen wahrscheinlich auf einem Array Paginieren. Dies ist einfach zu tun, siehe this. Hoffe das hat geholfen.

+0

Werfen Sie einen Blick auf meine Bearbeitung für weitere Erläuterungen und Code – rrivas