NoobOnRails

Ruby on Rails tips, tricks and hints for the aspiring rails hero.



Friday, February 10, 2006

It came from the mailing list...


Here are a few tibdits gleaded from my trying to read every post on the mialing list every day for the past few weeks (i'm way behind)...

Need to do some table striping but don't want to cramp your hand doing so?
Kevin posts about the Cycle helper...snippet

> You can also use the 'cycle' helper to do the table striping.
>
> Do something like this...
>
> ">

'cycle' is a predefined helper for doing table striping.

See the api docs for 'cycle'



How about a little refactoring? Say you have some code that iterates through all items in a given collection to save them all...instead of doing it the hard way, cut your work day in half with this tip...

> > On the other hand, you probably also need to make sure the records are
> > valid, so..
> > good_to_go = true
> > @collection.each do |record|
> > good_to_go = false unless record.save
> > end
> > flash[:notice] = "All records saved. Lunch break!" if good_to_go
> > etc, etc, etc.
>
> You could also do it like this:
>
> good_to_go = @collection.all? { |r| r.save }

0 Comments:

Post a Comment

<< Home