rails/rails — Issue #5358: Form Helper is outputting hidden fields as escaped HTML
Author: apolzon
Created: 2012-03-09 18:29:30+00:00
State: open

Since upgrading to Rails 3.1 (and I see this in 3.2 as well), when using the fields_for method in form_helper, if I have an if statement in the block, it will still output the hidden field for cases were no output is collected. (See gist for example).

Example output would be that in my form, it renders an input as expected, but for the case where my if-check returns false, it outputs the hidden field, which is not interpreted as HTML and results in the html tag being rendered to the user.

Screenshot: http://imageshack.us/photo/my-images/141/screenshot20120309at102.png/

Gist of example code: https://gist.github.com/2007890

I have worked around this by setting the @emitted_hidden_id intance variable to true in an else block.

I believe the proper fix is to have https://github.com/rails/rails/blob/3-2-stable/actionpack/lib/action_view/helpers/form_helper.rb#L607 check output.blank? instead of output (output is a SafeBuffer, so it will always not return nil)

I can supply a pull request if that is preferred, just let me know.