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
Leacockhttp://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.”