Forum: Ruby on Rails Is there a better way to do this? - newbie question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9a71afcb133b3c1a779a63835370582f?d=identicon&s=25 Dustin Anderson (dkanders)
on 2007-03-28 16:52
I'm deploying and running into problems that I didn't have in the
development environment...

In development, I have a section of code that says something like:

<% for charity in @charities %>
    <%= blah blah blah %>
<% end %>

and it works fine...

when I go to deployment, it throws an error saying it doesn't recognize
this class variable (because there weren't any charities entered in the
db).

When I put an 'if' statement in front of the 'for' loop, it works:

<% if @charities %>
<% for charity in @charities %>
    <%= blah blah blah %>
<% end %>

I kinda have 2 questions:
   1.  Why does not having the 'if' statement work in development, but
not in production?
   2.  Is there a cleaner way to write this code?

Thanks a HEAP :)
C4dc94c893471878a105761a9207f29b?d=identicon&s=25 Zack Chandler (Guest)
on 2007-03-28 17:06
(Received via mailing list)
Dustin,

You are probably not initializing @charities in the controller if
there are none.  I guess you could do this (see below) although I
would probably just check your controller code and always init
@charities...

<% for charity in (@charities || []) %>
  <%= blah blah blah %>
<% end %>

--
Zack Chandler
http://depixelate.com
2f9a03aa0fcfe945229cb6126eda2cb2?d=identicon&s=25 Philip Hallstrom (Guest)
on 2007-03-28 17:11
(Received via mailing list)
>
>
> I kinda have 2 questions:
>   1.  Why does not having the 'if' statement work in development, but
> not in production?

It works in production because you said there are rows in the production
database that get populated into @charities. So @charities contains
something.

>   2.  Is there a cleaner way to write this code?

How are you populating @charities?  Even in development with no records
@charities should get set to [] in which case that for loop should
work...

-philip
9a71afcb133b3c1a779a63835370582f?d=identicon&s=25 Dustin Anderson (dkanders)
on 2007-03-28 19:06
Philip Hallstrom wrote:
>>
> How are you populating @charities?  Even in development with no records
> @charities should get set to [] in which case that for loop should
> work...
>
> -philip

Thanks gang - I'm running edge rails and screwed that up... we didn't
have the vendor/rails directory deploying - so I think it was running on
an older version of rails that wasn't as "smart"...
This topic is locked and can not be replied to.