Truncate array?

I’m trying to truncate the output of an array to a certain number.

Kind of like pagination limits:
:per_page =>5

Can pagination for an array be done at the view level, without having to
declare it at the controller?

On Mon, Nov 13, 2006 at 06:49:33PM +0100, Jorge Nolla wrote:

I’m trying to truncate the output of an array to a certain number.

Kind of like pagination limits:
:per_page =>5

Can pagination for an array be done at the view level, without having to
declare it at the controller?

Sure. Do something like this:

irb(main):002:0> a = %w{ one two three four five size seven }
=> [“one”, “two”, “three”, “four”, “five”, “size”, “seven”]
irb(main):003:0> a[0…4]
=> [“one”, “two”, “three”, “four”, “five”]


Cheers,

  • Jacob A.

Sorry I’m kind of a newbie, I don’t know how exactly to implement that.
Here is the array:

<% for @post in @customer.post %>

<% end %>

could you show me how exactly do I have implement it.

Thank you much

Thanks Jacob; but both are giving me rerror messages. Don’t know what’s
going on.

On Mon, Nov 13, 2006 at 07:17:18PM +0100, Jorge Nolla wrote:

Sorry I’m kind of a newbie, I don’t know how exactly to implement that.
Here is the array:

I believe that you can simply insert the selection in this line:

    <% for @post in @customer.post %>

So that it becomes:

    <% for @post in @customer.post[0..4] %>

If that doesn’t work you could try this instead:

    <% @customer.post[0..4].each { |post| %>
    ...
    <% } %>


Cheers,

  • Jacob A.

Jacob A. wrote:

On Mon, Nov 13, 2006 at 07:17:18PM +0100, Jorge Nolla wrote:

I believe that you can simply insert the selection in this line:

    <% for @post in @customer.post %>

So that it becomes:

    <% for @post in @customer.post[0..4] %>

Does the customer model have a has_many :posts declaration in the class?
If so, then it should be:

     <% for @post in @customer.posts %>

right? It looks like @customer.post is only a single post object and in
that case, it wouldn’t respond to [].

I’m guessing that may be the problem based upon what you want to do and
the code here…

Maybe:

    <% for post in @customer.posts.collection[0..4] do %>
  1. <%= post.title %>
  2. <% end %>

Jorge Nolla wrote:

Thanks Jacob; but both are giving me rerror messages. Don’t know what’s
going on.

We can help you a lot better if you tell us what the error message is.
The errors themselves have quite a bit of meaning, it’s best to read
them carefully and understand them.

the customer model have a has_many :posts declaration in the class.

here is the working model of the site www.jnolla.com:3000/content/

I’m trying to only show 4 entries on the front page, per customer.

use

@array = @customer.post[0…4]

in the controller and then

<% for @item in @array -%>

try to avoid any type of assignment in the view
view variables should be treated as read only
if you need access to the other stuff later put it in
a different variable or an array of arrays

Jorge Nolla wrote:

Keynan P. wrote:

use

@array = @customer.post[0…4]

in the controller and then

<% for @item in @array -%>

try to avoid any type of assignment in the view
view variables should be treated as read only
if you need access to the other stuff later put it in
a different variable or an array of arrays

I’ve tried the above and it retuns this error:

You have a nil object when you didn’t expect it!
The error occured while evaluating nil.post

This is missing the parameter id for customer this is why it is
returning nill.

Keynan P. wrote:

use

@array = @customer.post[0…4]

in the controller and then

<% for @item in @array -%>

try to avoid any type of assignment in the view
view variables should be treated as read only
if you need access to the other stuff later put it in
a different variable or an array of arrays

I’ve tried the above and it retuns this error:

You have a nil object when you didn’t expect it!
The error occured while evaluating nil.post

Sorry I guess this didn’t work:

@customer.post[0…4]

But this did:

@customer.post[0…4]

It was missing one period. Thank you all for your help. It works!