rails pagination nested routes
I am attempting to put a contacts
paginated partial inside of a view for rooftops
. Rooftops has many contacts and also has a nested route allowing /rooftops/1/contacts
. When I try to create the will_paginate
links I get /rooftops/1
as the path rather than '/rooftops/1/contacts/'.
Is there a way to modify the pagination path with out creating my own pagination link view? I have tried going to the rdoc for will_paginate here but it goes to a godaddy page.
Any ideas?
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>
This creates /rooftops/1
and I'm not sure how to force it to /rooftops/1/contacts
.
---------------------EDIT---------------------
My routes is
resources :rooftops do
resources :contacts
end
I think I know where my problem is. Since my search function is within the model. I never actually t开发者_运维百科ouch the contacts
controller. I do the search from the rooftops
controller and call a partial from the contacts
view. I'll show what my views are.
<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>
This is the actual partial. As you can see, it never really goes through the contacts controller. Could this be the source of my problem?
<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>
Thanks for your help.
There are several places where you could go wrong. Perhaps you don't have your routes set correctly. The will_paginate
call should be in the contacts view, it it there? It is not clear from the snippet you gave.
The self.search
method in the Contact
model is very curious for me, this should be rather in the controller. It makes sense, the search method is related to the control of the Contact
. And have it as an instance method.
The route rooftops/:id/contacts
should lead to Contact controller. You might check whether it is so.
I suppose you need something like this:
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>
You probably need to paginate on an array. This is simple to do, please refer to this. Hope this helped.
精彩评论