我正在使用将像这样对引导程序进行分页:
我的模型:
class Customer < ActiveRecord::Base
attr_accessible :city, :country, :name, :street, :zip
//... more code
has_many :contacts, :dependent => :destroy, :autosave => true
end
class Contact < ActiveRecord::Base
attr_accessible :name, :notice, :phone
//... more code
belongs_to :customer
end
宝石文件:
gem "will_paginate-bootstrap"
能够在contacts/_pageable_contacts.html.erb中生成分页输出的部分:
<%
contacts = contacts.to_a.paginate( :page => params[:page] || 1, :per_page => 5 )
%>
<%= render(:partial => 'contacts/contacts', :locals => {:contacts => contacts})%>
<%= will_paginate contacts, renderer: BootstrapPagination::Rails, remote: true %>
在 contacts/create.js.erb 中调用:
<%
all = @contact.customer.contacts
%>
<%= render :partial => "contacts/pageable_contacts", :locals => { :contacts => all } %>
只要此部分已在 Contacts 控制器的上下文中呈现(例如 views/contacts/create.js.erb),will_paginate 生成的链接如下:
customers/3/contacts?page=1
customers/3/contacts?page=2
现在,我想在 customer/show.html.erb 中渲染这个部分,如下所示:
<%= render :partial => 'contacts/pageable_contacts', :object => @customer.contacts, :as => :contacts %>
但是 will_paginate 返回的链接是特定于客户的,而不是特定于联系人的。显然,will_paginate 访问 self(即 CustomerController)来构建链接。
customers/3?page=1
customers/3?page=2
如何告诉 partial 或 will_paginate 如何处理呢?我是否误解了 MVC、路线或其他任何东西的概念?我是否以错误的方式使用模型之间的关联?
任何帮助表示赞赏!谢谢你。戈尔比