# Objects in Views

**URL:** <https://www.ruby-forum.com/t/objects-in-views/174582>\
**Category:** Rails\
**Created:** [August 29, 2009, 1:40am UTC](https://www.ruby-forum.com/t/objects-in-views/174582 "2009-08-29T01:40:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhruvasagar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4bbf92/32.png) [@dhruvasagar](https://www.ruby-forum.com/u/dhruvasagar)\
**Post date:** [August 29, 2009, 1:40am UTC](https://www.ruby-forum.com/t/objects-in-views/174582/1 "2009-08-29T01:40:05Z")

</div>

Hi everyone,

I have recently experienced a strange behavior (strange from my  
knowledge)  
in rails.

In my controllers ‘new’ action, I am creating a few instance variables  
in  
the following manner :

@controllerModel = ControllerModel.new  
@model1 = Model1.all  
@model2 = Model2.all

in my ‘new’ view, I am using the @controllerModel to create the form for  
new  
and I am using the @model1 & @model2 within the form to populate 2 combo  
boxes in the following manner :

f.select :model1\_id, @model1.collect { |m1| [m1.name, m1.id] }

f.select :model2\_id, @model2.collect { |m2| [m2.name, m2.id] }

Now everything works fine, the form is generated fine, there are no  
errors  
even the form submit works fine when I ensure that all the required  
fields  
(ControllerModel validations).

But in the event some of my validations fail and in the controller I  
do a _render  
:action =\> ‘new’_ I am given an error / exception for @model1 & @model2  
being nil. I don’t understand the reason for the same, I am looking for  
an  
explanation for this.

As a work around of course I found that if I add the following to the  
view  
it works out fine, _but I don’t understand the flow fully, I want to  
know  
why the above mentioned error occurs_.

work around in ‘new’ view :

if @controllerModel.errors  
@model1 = Model1.all  
@model2 = Model2.all  
end

Thanks & Regards,  
Dhruva S…

Stephen  
Leacock[http://www.brainyquote.com/quotes/authors/s/stephen\_leacock.html](http://www.brainyquote.com/quotes/authors/s/stephen_leacock.html)

- “I detest life-insurance agents: they always argue that I shall some  
day  
die, which is not so.”

---

<div class="post-metadata">

**Author:** ![dhruvasagar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4bbf92/32.png) [@dhruvasagar](https://www.ruby-forum.com/u/dhruvasagar)\
**Post date:** [August 29, 2009, 9:54am UTC](https://www.ruby-forum.com/t/objects-in-views/174582/2 "2009-08-29T09:54:39Z")

</div>

2009/8/29 Dhruva S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> @model2 = Model2.all  
> even the form submit works fine when I ensure that all the required fields  
> (ControllerModel validations).
> 
> But in the event some of my validations fail and in the controller I do a  
> render :action =\> ‘new’ IÂ am given an error / exception for @model1 &  
> @model2 being nil. I don’t understand the reason for the same, I am looking  
> for an explanation for this.

The reason is that the @model1 and @model2 variables are only  
available for the initial render of new. They are setup by you in the  
controller, used in the view erb file to generate the html which is  
sent to the browser, then they are discarded. Therefore when you get  
validation failures and wish to show the view again you must set up  
the variables again. One solution is to have a private method of the  
controller called something like prepare\_for\_edit\_or\_new which you  
call where appropriate (you will likely have the same problem in edit)  
or, the solution I prefer, is to have Model1 and Model2 methods  
get\_selection\_data or similar that return the equivalent of  
@model1.collect { |m1| [m1.name, m1.id] } and then call this directly  
from the view, thus eliminating the need for the variables.

Colin

---

<div class="post-metadata">

**Author:** ![dhruvasagar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4bbf92/32.png) [@dhruvasagar](https://www.ruby-forum.com/u/dhruvasagar)\
**Post date:** [August 29, 2009, 10:02am UTC](https://www.ruby-forum.com/t/objects-in-views/174582/3 "2009-08-29T10:02:16Z")

</div>

On Sat, Aug 29, 2009 at 1:21 PM, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > @model2 being nil. I don’t understand the reason for the same, I am  
> > controller called something like prepare\_for\_edit\_or\_new which you  
> > call where appropriate (you will likely have the same problem in edit)  
> > or, the solution I prefer, is to have Model1 and Model2 methods  
> > get\_selection\_data or similar that return the equivalent of  
> > @model1.collect { |m1| [m1.name, m1.id] } and then call this directly  
> > from the view, thus eliminating the need for the variables.
> 
> Colin

Hi Colin,

Thank you very much for your input, I understand it now & yes it happens  
with edit as well.  
I think helpers is perhaps the best place to put the code for the  
@model.collect in.

Thanks & Regards,  
Dhruva S…

Pablo  
Picasso[http://www.brainyquote.com/quotes/authors/p/pablo\_picasso.html](http://www.brainyquote.com/quotes/authors/p/pablo_picasso.html)

- “Computers are useless. They can only give you answers.”

---

<div class="post-metadata">

**Author:** ![dhruvasagar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4bbf92/32.png) [@dhruvasagar](https://www.ruby-forum.com/u/dhruvasagar)\
**Post date:** [August 29, 2009, 10:23am UTC](https://www.ruby-forum.com/t/objects-in-views/174582/4 "2009-08-29T10:23:55Z")

</div>

2009/8/29 Dhruva S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > in
> > > 
> > > But in the event some of my validations fail and in the controller I do  
> > > sent to the browser, then they are discarded. Â Therefore when you get
> 
> Hi Colin,
> 
> Thank you very much for your input, I understand it now & yes it happens  
> with edit as well.  
> I think helpers is perhaps the best place to put the code for the  
> @model.collect in.

You could ask yourself who knows what are the valid options that a  
user may select? The answer may be the model, depending on your  
application. If so then put it in a model method. I am not talking  
about how to display the selection (drop down, radio buttons, check  
boxes or whatever), that is a decision for the view and it’s helpers.  
Possibly in the future there may be a need to disallow certain  
selections based on criteria in the db. In this case if a model  
method provides the selection the change will be in the model. If you  
have code like  
f.select :model1\_id, Model1.all.collect { |m1| [m1.name, m1.id] }  
in the view/helper then it becomes messy.  
f.select :model1\_id, Model1.get\_selection\_data  
is arguably better.

Colin

---

<div class="post-metadata">

**Author:** ![dhruvasagar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4bbf92/32.png) [@dhruvasagar](https://www.ruby-forum.com/u/dhruvasagar)\
**Post date:** [August 29, 2009, 10:30am UTC](https://www.ruby-forum.com/t/objects-in-views/174582/5 "2009-08-29T10:30:21Z")

</div>

That’s interesting, again thank you very much for you help.  
I too think that your idea is better 🙂

Thanks & Regards,  
Dhruva S…

Samuel  
Goldwyn[http://www.brainyquote.com/quotes/authors/s/samuel\_goldwyn.html](http://www.brainyquote.com/quotes/authors/s/samuel_goldwyn.html)

- “I’m willing to admit that I may not always be right, but I am never  
wrong.”
