简体   繁体   English

Rails分页嵌套路线

[英]rails pagination nested routes

I am attempting to put a contacts paginated partial inside of a view for rooftops . 我正在尝试将分页的contacts部分放在rooftops视图的内部。 Rooftops has many contacts and also has a nested route allowing /rooftops/1/contacts . 屋顶有许多接触点,并且还具有允许/rooftops/1/contacts的嵌套路径。 When I try to create the will_paginate links I get /rooftops/1 as the path rather than '/rooftops/1/contacts/'. 当我尝试创建will_paginate链接时,我将/rooftops/1作为路径而不是'/ 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. 我已经尝试过在rdoc上进行will_paginate的操作但它转到了godaddy页面。

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 . 这将创建/rooftops/1 ,我不确定如何将其强制为/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 touch the contacts controller. 我从未真正接触过contacts控制器。 I do the search from the rooftops controller and call a partial from the contacts view. 我从rooftops控制器进行搜索,然后从contacts视图中调用局部视图。 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? will_paginate调用应该在联系人视图中,在那儿吗? 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. Contact模型中的self.search方法对我来说很好奇,这应该在控制器中。 It makes sense, the search method is related to the control of the Contact . 这很有意义,搜索方法与Contact的控制有关。 And have it as an instance method. 并将其作为实例方法。

The route rooftops/:id/contacts should lead to Contact controller. 路线rooftops/:id/contacts应通向触点控制器。 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. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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