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 %> <tr> <td class="name"> <label for="<%=c.first%>"><%=c.first%></label></td> <td> <input id="<%=c.second%>" name="thing[<%=c.second %>]"type="text" /></td> </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> 36: 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 submitting? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 18 March 2010 13:43, ES <emstolfo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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")? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 18 March 2010 13:43, ES <emstolfo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 %> > <tr> > <td class="name"> > <label for="<%=c.first%>"><%=c.first%></label></td> > <td> > <input id="<%=c.second%>" name="thing[<%=c.second > %>]"type="text" /></td> > </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> > 36: > 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 > 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
yes I am going through create, should I make the @columns hash a class variable or just repopulate it in the create method? On Mar 18, 2:49 pm, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 18 March 2010 13:43, ES <emsto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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")?-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2010-Mar-18 14:05 UTC
Re: Re: hash in controller is nil after submitting form
On 18 March 2010 13:53, ES <emstolfo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 :-/ -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 18 March 2010 13:53, ES <emstolfo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Mar 18, 3:08 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 18 March 2010 13:53, ES <emsto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 18 March 2010 14:16, ES <emstolfo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
ok, thanks! On Mar 18, 3:24 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 18 March 2010 14:16, ES <emsto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-ru... > > Colin-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.