# Create action gives NoMethodError and nil.each

**URL:** <https://www.ruby-forum.com/t/create-action-gives-nomethoderror-and-nil-each/89477>\
**Category:** Rails\
**Created:** [March 20, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/create-action-gives-nomethoderror-and-nil-each/89477 "2007-03-20T20:29:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 20, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/create-action-gives-nomethoderror-and-nil-each/89477/1 "2007-03-20T20:29:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 21, 2007, 4:43am UTC](https://www.ruby-forum.com/t/create-action-gives-nomethoderror-and-nil-each/89477/2 "2007-03-21T04:43:22Z")

</div>

Sometimes this happens when you have code like this:

def create  
@foo = Foo.new(params[:foo])  
if @foo.valid?  
flash[:success] = ‘Foo created.’  
redirect\_to :action =\> ‘show’, :id =\> @foo  
else  
render :action =\> ‘new’  
end  
end

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’.

Example:

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

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

Hope this helps.

–  
Zack C.  
[http://depixelate.com](http://depixelate.com)

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 21, 2007, 5:47am UTC](https://www.ruby-forum.com/t/create-action-gives-nomethoderror-and-nil-each/89477/3 "2007-03-21T05:47:26Z")

</div>

That didn’t even cross my mind! Makes perfect sense. Thanks!
