# Controller instance variables

**URL:** <https://www.ruby-forum.com/t/controller-instance-variables/150144>\
**Category:** Rails\
**Created:** [October 4, 2008, 5:36pm UTC](https://www.ruby-forum.com/t/controller-instance-variables/150144 "2008-10-04T17:36:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mrpete](https://www.ruby-forum.com/u/mrpete)\
**Post date:** [October 4, 2008, 5:36pm UTC](https://www.ruby-forum.com/t/controller-instance-variables/150144/1 "2008-10-04T17:36:02Z")

</div>

hi, i’m very new to ruby and rails so be gentle. i am trying to set up  
a form that uses some validation but i’m having issues. i have a  
controller class that contains 2 methods: one for showing the form and  
another for processing the form. like so:

def show\_form  
store = Store.find\_by\_id(session[:store\_id])  
@all\_balls = store.balls  
@ball\_sizes = store.ball\_sizes  
end

def complete\_form  
@picked\_ball = session[:picked\_ball]  
if check\_nil @picked\_bread, ‘Please choose a ball.’  
return  
end

@picked\_ball\_size = session[:picked\_ball\_size]  
if check\_nil @picked\_ball\_size, ‘Please choose a ball size.’  
return  
end

# save stuff off into db

end

def check\_nil(var\_to\_check, message)  
if var\_to\_check.nil?  
flash[:notice] = message  
redirect\_to :back  
return true  
end  
false  
end

my problem is that if the user selects a ball but forgets to select a  
ball size, i go back to the form but it seems that i’ve lost all memory  
of @picked\_ball. i don’t want to user to have to select the ball again.  
i want it to be selected when the page is redisplayed with the error.  
do i have to put this stuff in the session? i hope i don’t because  
there are actually a lot more items on the page, i’ve cut it down so my  
post isn’t quite as long.

also, here is my form:

\<% form\_tag(:action =\> ‘complete\_form’) do %\>

### balls
 \<% for ball in @all\_balls %\> \<%= radio\_button\_tag 'picked\_ball', ball.name, @picked\_ball == ball.name %\> \<%= ball.name %\>  
 \<% end %\>

\<% for size in @all\_sizes %\>  
\<%= radio\_button\_tag ‘picked\_size’, size.name, @picked\_size ==  
size.name %\> \<%= size.name %\>  
  
\<% end %\>

\<% end %\>

thanks for any and all help.

-peter

---

<div class="post-metadata">

**Author:** ![mrpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mrpete](https://www.ruby-forum.com/u/mrpete)\
**Post date:** [October 4, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/controller-instance-variables/150144/2 "2008-10-04T17:57:25Z")

</div>

On Oct 4, 4:36 pm, Peter Ab [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> my problem is that if the user selects a ball but forgets to select a  
> ball size, i go back to the form but it seems that i’ve lost all memory  
> of @picked\_ball. i don’t want to user to have to select the ball again.  
> i want it to be selected when the page is redisplayed with the error.  
> do i have to put this stuff in the session? i hope i don’t because  
> there are actually a lot more items on the page, i’ve cut it down so my  
> post isn’t quite as long.

Typically one deals with this by re-rendering the form, not by  
redirecting. You’ll very often see this pattern:

@foo = Foo.new params[:foo]  
if @foo.save  
#success  
redirect\_to …  
else  
render :action =\> ‘new’  
end

Fred

---

<div class="post-metadata">

**Author:** ![mrpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mrpete](https://www.ruby-forum.com/u/mrpete)\
**Post date:** [October 4, 2008, 7:46pm UTC](https://www.ruby-forum.com/t/controller-instance-variables/150144/3 "2008-10-04T19:46:31Z")

</div>

On 4 Oct 2008, at 18:31, Peter Ab wrote:

> ok, i think i understand now after playing with it for a bit. it  
> looks  
> like the ‘render :action =\> new’ runs the code from the new method but  
> with all the context of the method from which it came where as  
> redirect\_to starts with a clean context.

It does not run the new method at all. It just renders the template  
that the new action would have rendered.  
So in a typical example, the new method would just do

def new  
@foo = Foo.new  
end  
ie the new method just starts out with an empty (all attributes set to  
the defaults) instance of Foo.

If you call render from your create method then instead of having that  
empty instance of Foo, you’ve got the one you created from the user  
input so all the fields the user had filled it would be filled in and  
so on.

> so, i still have 2 actions (methods) show and complete but if i find  
> an  
> error in complete, i now _render_ show again and have all the  
> context of  
> what i had in complete. when i used redirect\_to, it also went to show  
> but it cleared out the context.

That’s about right.

Fred

---

<div class="post-metadata">

**Author:** ![mrpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mrpete](https://www.ruby-forum.com/u/mrpete)\
**Post date:** [October 4, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/controller-instance-variables/150144/4 "2008-10-04T18:20:57Z")

</div>

Frederick C. wrote:

> Typically one deals with this by re-rendering the form, not by  
> redirecting. You’ll very often see this pattern:
> 
> @foo = Foo.new params[:foo]  
> if @foo.save  
> #success  
> redirect\_to …  
> else  
> render :action =\> ‘new’  
> end

thanks for the help, fred. i’m still a little confused. it is my  
understanding that “actions” are methods in the controller. would that  
code you posted be in a new def? i.e.

def new

# code you posted

end

are you saying i need to have the show and the handle performed by the  
same method? that doesn’t make sense to me as in the ‘show’ case, i’m  
not looking for anything but in the ‘complete’ case, i am. how do i  
know what i need to do if i have only one method doing both?

is there some tutorial somewhere you could point me to that goes in  
depth on forms and best practices in using them? a tutorial that goes  
into one controller method v. two; model object forms v. forms with just  
fields on them, etc.

thanks,

-peter

---

<div class="post-metadata">

**Author:** ![mrpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mrpete](https://www.ruby-forum.com/u/mrpete)\
**Post date:** [October 4, 2008, 7:31pm UTC](https://www.ruby-forum.com/t/controller-instance-variables/150144/5 "2008-10-04T19:31:39Z")

</div>

ok, i think i understand now after playing with it for a bit. it looks  
like the ‘render :action =\> new’ runs the code from the new method but  
with all the context of the method from which it came where as  
redirect\_to starts with a clean context.

so, i still have 2 actions (methods) show and complete but if i find an  
error in complete, i now _render_ show again and have all the context of  
what i had in complete. when i used redirect\_to, it also went to show  
but it cleared out the context.

thanks for the pointer, fred.

-peter
