hash in controller is nil after submitting form

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'] = [


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

  <% for c in @columns['thing_general'] do %>
       <td class="name">
       <label for="<%=c.first%>"><%=c.first%></label></td>
       <input id="<%=c.second%>" name="thing[<%=c.second
%>]"type="text" /></td>

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: <tr><td><hr/></td></tr>
37: <tbody>
38: <% for c in @columns['thing_general'] do %>
39: <tr>
40: <td class="name">
41: <label for="<%=c.first%>"><%=c.first%></label></td>

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

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")?

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.


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 :wink:

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 :-/

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?


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 -


ok, thanks!