# Partials views and instance variables go nil

**URL:** <https://www.ruby-forum.com/t/partials-views-and-instance-variables-go-nil/170219>\
**Category:** Rails\
**Created:** [June 14, 2009, 7:58am UTC](https://www.ruby-forum.com/t/partials-views-and-instance-variables-go-nil/170219 "2009-06-14T07:58:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [June 14, 2009, 7:58am UTC](https://www.ruby-forum.com/t/partials-views-and-instance-variables-go-nil/170219/1 "2009-06-14T07:58:14Z")

</div>

Hi everyone!

Model: [http://pastie.org/511290](http://pastie.org/511290)  
Controller: [http://pastie.org/511285](http://pastie.org/511285)  
View new: [http://pastie.org/511286](http://pastie.org/511286)  
View \_form: [http://pastie.org/511284](http://pastie.org/511284)

When I hit submit button there’s an error for usuarios(users in  
portuguese) instance. It does not seems to save usuarios even though it  
seems to be posted by the form.  
Error: [http://pastie.org/511292](http://pastie.org/511292)

Need some help here guys plz

---

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [June 14, 2009, 12:12pm UTC](https://www.ruby-forum.com/t/partials-views-and-instance-variables-go-nil/170219/2 "2009-06-14T12:12:36Z")

</div>

On Jun 14, 6:58 am, Bruno S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Error:[http://pastie.org/511292](http://pastie.org/511292)  
> Well the error happens because a validation fails and so it rerenders  
> the form, however your create action doesn’t setup @usuarios and so  
> the bit where you call collection\_select fails.

If you look at your parameters although the id is being submitted,  
it’s not being submitted in the right place in your params hash:  
you’ve got :usario =\> {:id =\> ‘6’}, whereas what you want is :venda =\>  
{:usario\_id =\> ‘6’, … }

This is because of how you are using collection\_select: you’re telling  
it that you are editing the id property of @usuario, but you’re not:  
you’re editing the usario\_id property of @venda. Fix that and it  
should work (why not call collection\_select on the form builder  
object ?)

Fred

---

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [June 14, 2009, 12:12pm UTC](https://www.ruby-forum.com/t/partials-views-and-instance-variables-go-nil/170219/3 "2009-06-14T12:12:37Z")

</div>

@usuarios is nil when you post the forms and there are validation  
errors (you’re not reloading them a the create action), here’s how to  
solve it:

\<%=collection\_select(:usuario, :id, Usuario.all, :id, :nome\_completo)%\>

- 

Maurício Linhares  
[http://codeshooter.wordpress.com/](http://codeshooter.wordpress.com/) | [http://twitter.com/mauriciojr](http://twitter.com/mauriciojr)

On Sun, Jun 14, 2009 at 2:58 AM, Bruno

---

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [June 14, 2009, 3:57pm UTC](https://www.ruby-forum.com/t/partials-views-and-instance-variables-go-nil/170219/4 "2009-06-14T15:57:40Z")

</div>

thanks!  
I got it working like this:  
[http://disciplinas.dcc.ufba.br/svn/MATA76/Trabalho20091/equipe13/trunk/lac/app/views/vendas/\_form.html.erb?view=log](http://disciplinas.dcc.ufba.br/svn/MATA76/Trabalho20091/equipe13/trunk/lac/app/views/vendas/_form.html.erb?view=log)

The problem is with nested models through ajax. When submit button is  
hitted, it claims that I didn’t choose a credit card(cartao\_de\_credito)  
for the payment(pagamento). How come?  
[http://disciplinas.dcc.ufba.br/svn/MATA76/Trabalho20091/equipe13/trunk/lac/app/views/vendas/\_pagamento.html.erb?view=log](http://disciplinas.dcc.ufba.br/svn/MATA76/Trabalho20091/equipe13/trunk/lac/app/views/vendas/_pagamento.html.erb?view=log)

you can browse all the code through svn pages

---

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [June 15, 2009, 2:19am UTC](https://www.ruby-forum.com/t/partials-views-and-instance-variables-go-nil/170219/5 "2009-06-15T02:19:47Z")

</div>

Nice!  
It worked.  
Last thing is that I tried using select\_tag in many ways, but none  
worked.  
By lines 9-12 in  
[http://disciplinas.dcc.ufba.br/svn/MATA76/Trabalho20091/equipe13/trunk/lac/app/views/vendas/\_pagamento.html.erb?view=log](http://disciplinas.dcc.ufba.br/svn/MATA76/Trabalho20091/equipe13/trunk/lac/app/views/vendas/_pagamento.html.erb?view=log)  
I just want a normal select tag with those 4 values.

Frederick C. wrote:

> On Jun 14, 2:57ï¿½pm, Bruno S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > thanks!  
> > I got it working like this:[http://disciplinas.dcc.ufba.br/svn/MATA76/Trabalho20091/equipe13/trun](http://disciplinas.dcc.ufba.br/svn/MATA76/Trabalho20091/equipe13/trun)…
> > 
> > The problem is with nested models through ajax. When submit button is  
> > hitted, it claims that I didn’t choose a credit card(cartao\_de\_credito)  
> > for the payment(pagamento). How come?[http://disciplinas.dcc.ufba.br/svn/MATA76/Trabalho20091/equipe13/trun](http://disciplinas.dcc.ufba.br/svn/MATA76/Trabalho20091/equipe13/trun)…
> 
> You’re using collection\_select wrong. EIther you use the form\_builder  
> version and do
> 
> f.collection\_select :attribute\_name, …
> 
> or you use the old version:
> 
> collection\_select :ivar\_name, :attribute\_name, …
> 
> but not some mix of the two as you have there
> 
> Fred

---

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [June 14, 2009, 4:43pm UTC](https://www.ruby-forum.com/t/partials-views-and-instance-variables-go-nil/170219/6 "2009-06-14T16:43:55Z")

</div>

On Jun 14, 2:57 pm, Bruno S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> thanks!  
> I got it working like this:[http://disciplinas.dcc.ufba.br/svn/MATA76/Trabalho20091/equipe13/trun](http://disciplinas.dcc.ufba.br/svn/MATA76/Trabalho20091/equipe13/trun)…
> 
> The problem is with nested models through ajax. When submit button is  
> hitted, it claims that I didn’t choose a credit card(cartao\_de\_credito)  
> for the payment(pagamento). How come?[http://disciplinas.dcc.ufba.br/svn/MATA76/Trabalho20091/equipe13/trun](http://disciplinas.dcc.ufba.br/svn/MATA76/Trabalho20091/equipe13/trun)…

You’re using collection\_select wrong. EIther you use the form\_builder  
version and do

f.collection\_select :attribute\_name, …

or you use the old version:

collection\_select :ivar\_name, :attribute\_name, …

but not some mix of the two as you have there

Fred
