# Hash in controller is nil after submitting form

**URL:** <https://www.ruby-forum.com/t/hash-in-controller-is-nil-after-submitting-form/185346>\
**Category:** Rails\
**Created:** [March 18, 2010, 2:44pm UTC](https://www.ruby-forum.com/t/hash-in-controller-is-nil-after-submitting-form/185346 "2010-03-18T14:44:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [March 18, 2010, 2:44pm UTC](https://www.ruby-forum.com/t/hash-in-controller-is-nil-after-submitting-form/185346/1 "2010-03-18T14:44:48Z")

</div>

I’m trying to create a data entry form but getting an error when it  
sumbmits. I am using an hash to get the names of the fields definied  
in the controller’s “new” function:

def new  
@thing = thing.new  
@columns = Hash.new

```
@columns['thing_general'] = [
        ["title","name"],
        ["number","num"],

```

etc…

and then I access it in the object’s new.html.erb:

\<% for c in @columns[‘thing\_general’] do %\>

  
  
\<%=c.first%\>  
  
\<input id="\<%=c.second%\>" name="thing[\<%=c.second  
%\>]"type=“text” /\>  

the first time I load the page, the correct form displays but then  
when I click submit, I get this error :

You have a nil object when you didn’t expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occurred while evaluating nil.[]

Extracted source (around line #38):

35:

* * *
  
36:  
37:   
38: \<% for c in @columns[‘thing\_general’] do %\>  
39:   
40:   
41: \<%=c.first%\>

why would it get a nil object when I submit the form (after working  
the first time) and why is it trying to load the same page after  
submitting?

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [March 18, 2010, 2:51pm UTC](https://www.ruby-forum.com/t/hash-in-controller-is-nil-after-submitting-form/185346/2 "2010-03-18T14:51:50Z")

</div>

On 18 March 2010 13:43, ES [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the first time I load the page, the correct form displays but then  
> when I click submit, I get this error :
> 
> You have a nil object when you didn’t expect it!  
> You might have expected an instance of ActiveRecord::Base.  
> The error occurred while evaluating nil.[]
> 
> why would it get a nil object when I submit the form (after working  
> the first time) and why is it trying to load the same page after  
> submitting?

When you click submit, you’re probably going through the “create”  
method (unless you’ve set the form to go somewhere else)… does the  
@columns[‘thing\_general’] value get populated there (you’ve shown the  
code for the “new” method, but not “create”)?

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [March 18, 2010, 2:52pm UTC](https://www.ruby-forum.com/t/hash-in-controller-is-nil-after-submitting-form/185346/3 "2010-03-18T14:52:25Z")

</div>

On 18 March 2010 13:43, ES [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> ["number","num"],
> <input id="<%=c.second%>" name="thing[<%=c.second
> 
> ```
> 
> Extracted source (around line #38):
> 
> why would it get a nil object when I submit the form (after working  
> the first time) and why is it trying to load the same page after  
> submitting?

I presume that when you click it is calling a controller action, then  
for whatever reason rendering the page again, but this time @columns  
is not setup, hence the error. Have a look at the action called by  
the submit and see what you have told it to do. Also have a look in  
development.log and you may get a clue. Guessing at what the code may  
do, possibly the submit requests a save that fails so you are  
re-rendering the page to allow the user to make a correction, but you  
have not setup @columns.

Colin

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [March 18, 2010, 2:55pm UTC](https://www.ruby-forum.com/t/hash-in-controller-is-nil-after-submitting-form/185346/4 "2010-03-18T14:55:21Z")

</div>

yes I am going through create, should I make the @columns hash a class  
variable or just repopulate it in the create method?

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [March 18, 2010, 3:08pm UTC](https://www.ruby-forum.com/t/hash-in-controller-is-nil-after-submitting-form/185346/5 "2010-03-18T15:08:49Z")

</div>

On 18 March 2010 13:53, ES [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> yes I am going through create, should I make the @columns hash a class  
> variable or just repopulate it in the create method?

You can do it however you like; you just need to make sure it’s not nil  
😉

Personally… I would make a decision based on the object and the  
purpose:

- I _doubt_ it’s really likely to be best as a class variable
- repopulate it if that’s easiest, but consider refactoring the  
population out to a private method to reuse it
- Could it be that this variable may really make more sense as a  
constant?
- Maybe other factors come into play that aren’t evident from your  
posting…

To be honest… I’m not sure I like the idea of the controller telling  
the view which fields to display - maybe this functionality would be  
better off in a view helper - but that’s a different conversation, and  
it’s your code :-/

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [March 18, 2010, 3:18pm UTC](https://www.ruby-forum.com/t/hash-in-controller-is-nil-after-submitting-form/185346/6 "2010-03-18T15:18:11Z")

</div>

hmmm, I’m totally new to this, what exactly is a helper used for? For  
these kinds of constant-type things in a view?

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [March 18, 2010, 3:10pm UTC](https://www.ruby-forum.com/t/hash-in-controller-is-nil-after-submitting-form/185346/7 "2010-03-18T15:10:35Z")

</div>

On 18 March 2010 13:53, ES [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> yes I am going through create, should I make the @columns hash a class  
> variable or just repopulate it in the create method?

You could populate it in a before or after\_filter on those actions. I  
have to say that the whole thing looks a bit odd. Are you sure it  
should not be in a view helper for example?

Colin

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [March 18, 2010, 3:26pm UTC](https://www.ruby-forum.com/t/hash-in-controller-is-nil-after-submitting-form/185346/8 "2010-03-18T15:26:23Z")

</div>

On 18 March 2010 14:16, ES [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hmmm, I’m totally new to this, what exactly is a helper used for? For  
> these kinds of constant-type things in a view?

This is a reasonable introduction -  
[http://paulsturgess.co.uk/articles/show/49-using-helper-methods-in-ruby-on-rails](http://paulsturgess.co.uk/articles/show/49-using-helper-methods-in-ruby-on-rails)

Colin

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [March 18, 2010, 3:28pm UTC](https://www.ruby-forum.com/t/hash-in-controller-is-nil-after-submitting-form/185346/9 "2010-03-18T15:28:44Z")

</div>

ok, thanks!
