开发者

Rails - WillPaginate Conflict

I'm trying to paginate the messages a group has.

class GroupsController < ApplicationController
...
def show
...
@message = @group.messages.paginate(:page => params[:page])
end

  <% unless @group.messages.empty? %>
  <table class="messages" summary="Messages from Group">
  <tr>
  <td class="messages">
  <span class="content"><%= messages.content%></span>
  <span class="timestamp">
  Posted <%= time_ago_in_words(messages.created_at) %> ago.
  </span>
  </td>
  </tr>
 </table>
 <%= will_paginate @messagess%>
 <%end %>

But when I try to open my group#show page, gives me the error: undefined method 'content' for #<WillPaginate::Collection:0x31a67f0>, but the line the error gives is the line where I create a new开发者_开发百科 Message, that is in the group#show page as well:

 <%= f.text_area :content, :rows=>5 , :cols=>49 %> 

Any ideas?


You need to loop over the messages and print each one out individually.

Controller:

def show
  @messages = @group.messages.paginate(:page => params[:page])
end

View:

<% unless @messages.empty? %>
  <table class="messages" summary="Messages from Group">

    <% @messages.each do |message| %>
      <tr>
        <td class="messages">
          <span class="content"><%= message.content %></span>
          <span class="timestamp">
            Posted <%= time_ago_in_words(message.created_at) %> ago.
          </span>
        </td>
      </tr>
    <% end %>

  </table>
  <%= will_paginate @messagess%>
<% end %>

To prevent the error on your form you will need to pass it a single instance of a message. e.g.

form_for(Message.new) do
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜