【问题标题】:rails pagination nested routesrails 分页嵌套路由
【发布时间】:2010-11-24 18:55:10
【问题描述】:

我正在尝试将contacts 分页部分放在rooftops 的视图中。 Rooftops 有很多联系人,并且还有一个允许/rooftops/1/contacts 的嵌套路由。当我尝试创建will_paginate 链接时,我得到/rooftops/1 作为路径而不是'/rooftops/1/contacts/'。

有没有办法在不创建我自己的分页链接视图的情况下修改分页路径?我已经尝试去 rdoc 获取 will_paginate here 但它进入了一个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>

感谢您的帮助。

【问题讨论】:

标签: ruby-on-rails will-paginate nested-routes


【解决方案1】:

有几个地方可能会出错。也许您没有正确设置路线。 will_paginate 呼叫应该在联系人视图中,它在那里?从你给的sn-p看不清楚。

Contact 模型中的self.search 方法对我来说很好奇,这应该是在控制器中。有道理,搜索方法与Contact的控制有关。并将其作为实例方法。

路由rooftops/:id/contacts 应该指向Contact 控制器。你可以检查一下是不是这样。

我想你需要这样的东西:

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>

您可能需要对数组进行分页。这个很简单,请参考this。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    相关资源
    最近更新 更多