please help; form_for passes nil

I have an update button that doesn't update. Watching the server during the attempt, I see:

Processing OrvesController#update (for 128.119.60.171 at 2010-04-02 13:13:29) [PUT]   Parameters: {"orf"=>nil, "commit"=>"Update", "id"=>"19544"}

orf is my model I'm trying to update in the database. Why is it nil?

The view for the page with the update button is called edit.html.erb and contains:

<h1>Editing orf</h1> <table>   <% form_for @orf do |f| %></td>     <td><%= f.error_messages %>     <td><%= h @orf.locus_tag %></td>     <td><%= f.text_field 'current_annotation', :size => 50 %></td>     <td><%= f.submit "Update" %></td>     <td><%= button_to "Undo last change", :action => :undo_last_change, :id => @orf %></td>   <% end %>

orf's attributes locus_tag and current_annotation are being correctly displayed.

Here's the update method:

  def update     @orf = Orf.find(params[:id])     if @orf.update_attributes(params[:orf])       flash[:notice] = 'Orf was successfully updated.'       redirect_to orf_path     else       flash[:notice] = 'Update failed.'       redirect_to orf_path     end   end

routes.rb contains the line:   map.resources :orves

No error message appears in the web browser. The update just doesn't change anything.

I'm running Rails 2.3.0.

Any idea what I'm doing wrong?

I have an update button that doesn't update. Watching the server during the attempt, I see:

Processing OrvesController#update (for 128.119.60.171 at 2010-04-02 13:13:29) [PUT] Parameters: {"orf"=>nil, "commit"=>"Update", "id"=>"19544"}

orf is my model I'm trying to update in the database. Why is it nil?

It's probably worth taking a look at the raw data posted by the browser - if it is malformed in some way that could be causing rails to parse it in a funny way. It probably doesn't help that your html is malformed (a td must be contained in a tr or similar, i'm pretty sure you can't stick a form in a table like that and the button_to will create a second form (ie you'll have a form nested in another form)

Fred

I have an update button that doesn't update. Watching the server during the attempt, I see:

Processing OrvesController#update (for 128.119.60.171 at 2010-04-02 13:13:29) [PUT] Parameters: {"orf"=>nil, "commit"=>"Update", "id"=>"19544"}

orf is my model I'm trying to update in the database. Why is it nil?

It's probably worth taking a look at the raw data posted by the browser - if it is malformed in some way that could be causing rails to parse it in a funny way. It probably doesn't help that your html is malformed (a td must be contained in a tr or similar, i'm pretty sure you can't stick a form in a table like that and the button_to will create a second form (ie you'll have a form nested in another form)

Fred

As Fred has pointed out your html is invalid. I find the html validator add-in for firefox excellent as it checks my html as I develop the app, making sure I do not make such errors. Also you can paste your html into the w3c htlm validator (find with google if necessary) to check it.

It is a pain that forms cannot be wrapped around table cells but that is the way it is, some browsers will cope with it but it cannot be guaranteed. A form can only wrap a whole table or fit entirely within a cell.

Colin

Thanks for the reply. I've now fixed the html (removed the table), but it didn't fix the problem. The edit view is: <h1>Editing orf</h1> <% form_for @orf do |f| %>   <%= f.error_messages %>   <%= h @orf.locus_tag %>   <%= f.text_field 'current_annotation', :size => 50 %>   <%= f.submit "Update" %>   <%= button_to "Undo last change", :action => :undo_last_change, :id => @orf %> <% end %>

I'm not sure what you mean about taking a look at the raw data posted by the browser to see if it is malformed. I turned on Firebug and looked at what it was trying to do when I pressed my "Update" button and I don't see anything wrong. It shows:

_method put commit Update orf[current_annotation] hypothetical proteinski

That looks OK to me , but I don't really know what I should be looking for. I was trying to change the current_annotation from hypothetical protein to hypothetical proteinski, so it looks like it tried. But the change doesn't happen. Any suggestion of how to troubleshoot this further?

Thanks for the reply. I've now fixed the html (removed the table), but it didn't fix the problem. The edit view is: <h1>Editing orf</h1> <% form_for @orf do |f| %> <%= f.error_messages %> <%= h @orf.locus_tag %> <%= f.text_field 'current_annotation', :size => 50 %> <%= f.submit "Update" %> <%= button_to "Undo last change", :action => :undo_last_change, :id => @orf %> <% end %>

I'm not sure what you mean about taking a look at the raw data posted by the browser to see if it is malformed. I turned on Firebug and looked at what it was trying to do when I pressed my "Update" button and I don't see anything wrong. It shows:

Try the html validator addon for firefox as I suggested, or/and the w3c html validator (google will find it). Then you know your html is valid.

Then have a look at the debugging guide at http://guides.rubyonrails.org/ which will show how to break into your code using ruby-debug (and other debugging techniques) to find out what is going wrong. Also have a look at the params in the log and convince yourself the data are there as expected.

Colin

Colin

Thanks for the reply. I've now fixed the html (removed the table), but it didn't fix the problem. The edit view is:

You've still got a form nested inside a form (button_to creates a form)

Fred

dirtbug wrote:

Fred

Thanks. I took out the whole button_to line, but it still doesn't work. I now have: <h1>Editing orf</h1> <% form_for @orf do |f| %>   <%= f.error_messages %>   <%= h @orf.locus_tag %>   <%= f.text_field 'current_annotation', :size => 50 %>   <%= f.submit "Update" %> <% end %>

Still doesn't update.

Fred

Thanks. I took out the whole button_to line, but it still doesn't work. I now have: <h1>Editing orf</h1> <% form_for @orf do |f| %> <%= f.error_messages %> <%= h @orf.locus_tag %> <%= f.text_field 'current_annotation', :size => 50 %> <%= f.submit "Update" %> <% end %>

Still doesn't update.

Can you confirm that the html now validates successfully? If so then please post the html for the form and what is seen in the log when the button is clicked.

Colin

Colin,

I don't know how to determine if validation is happening or whether it is successful.

The edit.html.erb is:

<h1>Editing orf</h1> <% form_for @orf do |f| %>   <%= f.error_messages %>   <%= h @orf.locus_tag %>   <%= f.text_field 'current_annotation', :size => 50 %>   <%= f.submit "Update" %> <% end %>

the update method is:

  def update     @orf = Orf.find(params[:id])     if @orf.update_attributes(params[:orf])     #if @orf.update_attribute(:current_annotation, @orf[ "current_annotation" ])       flash[:notice] = 'Orf was successfully updated.'       redirect_to orf_path     else       flash[:notice] = 'Update failed.'       redirect_to orf_path     end   end

and the log (upon clicking the button shows:

Processing OrvesController#update (for 128.119.60.171 at 2010-04-06 11:52:39) [PUT]   Parameters: {"orf"=>nil, "commit"=>"Update", "id"=>"1705"}   Orf Columns (26.0ms) SHOW FIELDS FROM `orves`   Orf Load (1.1ms) SELECT * FROM `orves` WHERE (`orves`.`id` = 1705)   SQL (0.2ms) BEGIN   SQL (0.2ms) COMMIT Redirected to /orves/1705 Completed in 52ms (DB: 28) | 302 Found [http:// andromeda.micro.umass.edu/orves/1705]   SQL (0.4ms) SET NAMES 'utf8'   SQL (0.2ms) SET SQL_AUTO_IS_NULL=0

Processing OrvesController#show (for 128.119.60.171 at 2010-04-06 11:52:39) [GET]   Parameters: {"id"=>"1705"}   Orf Columns (17.7ms) SHOW FIELDS FROM `orves`   Orf Load (1.1ms) SELECT * FROM `orves` WHERE (`orves`.`id` = 1705) Rendering orves/show Completed in 83ms (View: 45, DB: 19) | 200 OK [http:// andromeda.micro.umass.edu/orves/1705]

By the way, I commented out the line in application_controller.rb that had:   #protect_from_forgery # See ActionController::RequestForgeryProtection for details

Thanks.

Colin,

I don't know how to determine if validation is happening or whether it is successful.

I have told you twice previously. Install the html tidy plugin in firefox, and/or paste the whole html of the page (View, Page Source in browser, or similar and copy the text) into the w3c html validator (google w3c html validator to find it).

You have not shown us the html of the form (View Page Source again to see it) that I asked for.

Colin

Colin,

I don't know how to determine if validation is happening or whether it is successful.

I have told you twice previously. Install the html tidy plugin in firefox, and/or paste the whole html of the page (View, Page Source in browser, or similar and copy the text) into the w3c html validator (google w3c html validator to find it).

Sorry, that is the Html Validator plugin for FF

Colin

Colin,

Here is the html of the form, before I press the update button:

<h1>Editing orf</h1> <form action="/orves/1705" class="edit_orf" id="edit_orf_1705" method="post"><div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div>

  Gura_0317   <input id="orf_current_annotation" name="orf[current_annotation]" size="50" type="text" value="hypothetical protein" />   <input id="orf_submit" name="commit" type="submit" value="Update" /> </form>

When I paste it into the html validator (on their web page), I get:

Validation Output: 2 Errors

   1. Error Line 1, Column 1: no document type declaration; will parse without validation

      <h1>Editing orf</h1>

      ✉

      The document type could not be determined, because the document had no correct DOCTYPE declaration. The document does not look like HTML, therefore automatic fallback could not be performed, and the document was only checked against basic markup syntax.

      Learn how to add a doctype to your document from our FAQ, or use the validator's Document Type option to validate your document against a specific Document Type.    2. Error Line 2, Column 77: document type does not allow element "FORM" here

      …_orf" id="edit_orf_1705" method="post"><div style="margin: 0;padding:0"><input …

      ✉

      The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed).

      One common cause for this error is the use of XHTML syntax in HTML documents. Due to HTML's rules of implicitly closed elements, this error can create cascading effects. For instance, using XHTML's "self-closing" tags for "meta" and "link" in the "head" section of a HTML document may cause the parser to infer the end of the "head" section and the beginning of the "body" section (where "link" and "meta" are not allowed; hence the reported error).    3. Warning Line 2, Column 158: NET-enabling start-tag requires SHORTTAG YES

      …e="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div>

      ✉

      The sequence <FOO /> can be interpreted in at least two different ways, depending on the DOCTYPE of the document. For HTML 4.01 Strict, the '/' terminates the tag <FOO (with an implied '>'). However, since many browsers don't interpret it this way, even in the presence of an HTML 4.01 Strict DOCTYPE, it is best to avoid it completely in pure HTML documents and reserve its use solely for those written in XHTML.    4. Warning Line 5, Column 120: NET-enabling start-tag requires SHORTTAG YES

      …orf[current_annotation]" size="50" type="text" value="hypothetical protein" />

      ✉

      The sequence <FOO /> can be interpreted in at least two different ways, depending on the DOCTYPE of the document. For HTML 4.01 Strict, the '/' terminates the tag <FOO (with an implied '>'). However, since many browsers don't interpret it this way, even in the presence of an HTML 4.01 Strict DOCTYPE, it is best to avoid it completely in pure HTML documents and reserve its use solely for those written in XHTML.    5. Warning Line 6, Column 68: NET-enabling start-tag requires SHORTTAG YES

        <input id="orf_submit" name="commit" type="submit" value="Update" />

      ✉

      The sequence <FOO /> can be interpreted in at least two different ways, depending on the DOCTYPE of the document. For HTML 4.01 Strict, the '/' terminates the tag <FOO (with an implied '>'). However, since many browsers don't interpret it this way, even in the presence of an HTML 4.01 Strict DOCTYPE, it is best to avoid it completely in pure HTML documents and reserve its use solely for those written in XHTML.

Colin,

I added a layout file that had gone missing and now it validates. The html;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;

<html xmlns="http://www.w3.org/1999/xhtml&quot; xml:lang="en" lang="en"> <head>   <meta http-equiv="content-type" content="text/html;charset=UTF-8" />   <title>Orves: edit</title>   <link href="/stylesheets/baby.css?1270578037" media="screen" rel="stylesheet" type="text/css" /> </head> <body>

<p style="color: green"></p>

<h1>Editing orf</h1>

<form action="/orves/1705" class="edit_orf" id="edit_orf_1705" method="post"><div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div>

  Gura_0317   <input id="orf_current_annotation" name="orf[current_annotation]" size="50" type="text" value="hypothetical protein" />   <input id="orf_submit" name="commit" type="submit" value="Update" /> </form>

</body> </html>

And the validation report:

HTML Validator     0 errors, 0 warnings

The validated page has no errors, no warning found by the SGML Parser and HTML Tidy.

But it still doesn't update the database.

Colin,

I added a layout file that had gone missing and now it validates. The html;

Well, that was a worthwhile exercise, even if it was not the cause of the problem.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;

<html xmlns="http://www.w3.org/1999/xhtml&quot; xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> <title>Orves: edit</title> <link href="/stylesheets/baby.css?1270578037" media="screen" rel="stylesheet" type="text/css" /> </head> <body>

<p style="color: green"></p>

<h1>Editing orf</h1>

<form action="/orves/1705" class="edit_orf" id="edit_orf_1705" method="post"><div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div>

Gura_0317 <input id="orf_current_annotation" name="orf[current_annotation]" size="50" type="text" value="hypothetical protein" /> <input id="orf_submit" name="commit" type="submit" value="Update" /> </form>

</body> </html>

And the validation report:

HTML Validator 0 errors, 0 warnings

The validated page has no errors, no warning found by the SGML Parser and HTML Tidy.

But it still doesn't update the database.

The other thing I asked a few mails ago was what is shown in the log when you click submit, now that you have corrected several problems.

Colin

Colin,

The log now shows:

Processing OrvesController#update (for 128.119.60.171 at 2010-04-06 14:48:28) [PUT]   Parameters: {"orf"=>nil, "commit"=>"Update", "id"=>"1705"}   e[4;36;1mOrf Columns (22.6ms)e[0m e[0;1mSHOW FIELDS FROM `orves`e[0m   e[4;35;1mOrf Load (0.9ms)e[0m e[0mSELECT * FROM `orves` WHERE (`orves`.`id` = 1705) e[0m   e[4;36;1mSQL (0.2ms)e[0m e[0;1mBEGINe[0m   e[4;35;1mSQL (0.3ms)e[0m e[0mCOMMITe[0m Redirected to /orves/1705 Completed in 49ms (DB: 25) | 302 Found [http:// andromeda.micro.umass.edu/orves/1705]   e[4;36;1mSQL (0.4ms)e[0m e[0;1mSET NAMES 'utf8'e[0m   e[4;35;1mSQL (0.3ms)e[0m e[0mSET SQL_AUTO_IS_NULL=0e[0m

Processing OrvesController#show (for 128.119.60.171 at 2010-04-06 14:48:28) [GET]   Parameters: {"id"=>"1705"}   e[4;36;1mOrf Columns (18.5ms)e[0m e[0;1mSHOW FIELDS FROM `orves`e[0m   e[4;35;1mOrf Load (0.9ms)e[0m e[0mSELECT * FROM `orves` WHERE (`orves`.`id` = 1705) e[0m Rendering template within layouts/orves Rendering orves/show Completed in 96ms (View: 58, DB: 20) | 200 OK [http:// andromeda.micro.umass.edu/orves/1705]

Colin,

The log now shows:

Processing OrvesController#update (for 128.119.60.171 at 2010-04-06 14:48:28) [PUT] Parameters: {"orf"=>nil, "commit"=>"Update", "id"=>"1705"}

Well, amazing, after fixing multiple problems the fundamental issue remains unaffected! Can anyone else see why the orf parameter is nil?

I have always used <%= f.text_field :field_name ... whereas I see you have <%= f.text_field 'field_name' but I would be surprised if this were the problem. The html looks ok.

Does it work when creating a new orf record?

Colin

Colin,

I don't have a new method or view, as the db gets populated by a script that directly accesses the MySQL db. Thus the db records aren't created by Rails at all, which might be the source of the problem I don't know. The thing is, it all worked fine before we upgraded to Rails 2. I'll try to add a new method and view and try it out.

Have you had a look (with tcpdump, Firebug etc. ) at exactly what your form sends to the server?

Fred

Fred,

Is this what you meant? With Firebug open, I go to the form as a user would, change the annotation "hypothetical protein" to "hypothetical proteinski", then click the update button.

In firebug, I see a new POST request. I can examine it with any of four Firebug tabs (Headers, Post, Response, HTML). Using the "post" tab, I see:

_method put commit Update orf[current_annotation] hypothetical proteinski

Under the "Request" tab in Firebug, I can examine the Request header, which is:

Host andromeda.micro.umass.edu:8080 User-Agent Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv: 1.9.1.9) Gecko/20100315 Firefox/3.5.9 Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language en-us,en;q=0.5 Accept-Encoding gzip,deflate Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive 300 Connection keep-alive Referer http://andromeda.micro.umass.edu:8080/orves/1705/edit Cookie ; ; __utma=198765611.347868135.1233253686.1269352816.1269440269.55; __utmz=198765611.1269352816.54.21.utmccn=(organic)|utmcsr=google| utmctr=umass+map|utmcmd=organic; ["_baby_session=BAh7BzoPc2Vzc2lvbl9pZCIlMTYxODY1MmVkZDMyOWU3MmJiNjAyM2ZiOTY5NmRmNTNJIgpmbGFzaAY6DWVuY29kaW5nIg1VUy1BU0NJSUlDOidBY3Rpb25Db250cm9sbGVyOjpGbGFzaDo6Rmxhc2hIYXNoewAGOgpAdXNlZHsA-- ee8a24f1c308ec43a2b838d45cdb58e9a777ed4c;