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

On 18 March 2010 13:43, ES [email protected] 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”)?

On 18 March 2010 13:43, ES [email protected] 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

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

On 18 March 2010 13:53, ES [email protected] 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
: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 :-/

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

On 18 March 2010 13:53, ES [email protected] 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

On 18 March 2010 14:16, ES [email protected] 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

Colin

ok, thanks!