Zeroz
Zeroz

Reputation: 125

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?

Upvotes: 1

Views: 79

Answers (1)

Douglas F Shearer
Douglas F Shearer

Reputation: 26518

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

Upvotes: 2

Related Questions