[英]rails pagination nested routes
我正在嘗試將分頁的contacts
部分放在rooftops
視圖的內部。 屋頂有許多接觸點,並且還具有允許/rooftops/1/contacts
的嵌套路徑。 當我嘗試創建will_paginate
鏈接時,我將/rooftops/1
作為路徑而不是'/ rooftops / 1 / contacts /'。
有沒有一種方法可以修改分頁路徑而無需創建自己的分頁鏈接視圖? 我已經嘗試過在rdoc上進行will_paginate的操作,但它轉到了godaddy頁面。
有任何想法嗎?
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>
這將創建/rooftops/1
,我不確定如何將其強制為/rooftops/1/contacts
。
- - - - - - - - - - -編輯 - - - - - - - - - - -
我的路線是
resources :rooftops do
resources :contacts
end
我想我知道我的問題在哪里。 由於我的搜索功能在模型內。 我從未真正接觸過contacts
控制器。 我從rooftops
控制器進行搜索,然后從contacts
視圖中調用局部視圖。 我將展示我的觀點。
<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>
這是實際的部分。 如您所見,它從未真正通過通訊錄控制器。 這可能是我的問題根源嗎?
<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>
謝謝你的幫助。
您可能會在幾個地方出錯。 也許您的路線設置不正確。 will_paginate
調用應該在聯系人視圖中,在那兒嗎? 從您提供的片段中還不清楚。
Contact
模型中的self.search
方法對我來說很好奇,這應該在控制器中。 這很有意義,搜索方法與Contact
的控制有關。 並將其作為實例方法。
路線rooftops/:id/contacts
應通向觸點控制器。 您可能會檢查是否是這樣。
我想你需要這樣的東西:
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>
您可能需要對數組進行分頁。 這是簡單的事情,請參考這個 。 希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.