Forum: Ruby on Rails create action gives NoMethodError and nil.each

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
1df9fc8ddf084661265bbae74a8d0b43?d=identicon&s=25 jko170 (Guest)
on 2007-03-20 20:29
(Received via mailing list)
I have two actions, create and index. The index view has a list of
find(:all) objects and also a form for creating new objects. If the
form is valid upon creation, everything works (it is redirected to the
index page with the new object listed). If validation fails, I get the
error. My questions is, is it possible to do something like this?
C4dc94c893471878a105761a9207f29b?d=identicon&s=25 Zack Chandler (Guest)
on 2007-03-21 04:43
(Received via mailing list)
Sometimes this happens when you have code like this:

def create
  @foo =[:foo])
  if @foo.valid?
    flash[:success] = 'Foo created.'
    redirect_to :action => 'show', :id => @foo
    render :action => 'new'

If it works on a successful create but not on a render to 'new' then
you are likely loading objects in the 'new' action that is not loaded
in 'create'.


def new
  @foo =
  @bars = Bar.find(:all)

When you render the 'new' template from the create action you may need
to reload the @bars (in this example).

Hope this helps.

Zack Chandler
1df9fc8ddf084661265bbae74a8d0b43?d=identicon&s=25 jko170 (Guest)
on 2007-03-21 05:47
(Received via mailing list)
That didn't even cross my mind! Makes perfect sense. Thanks!
This topic is locked and can not be replied to.