2009-11-06

Adding the accept-charset attribute to HTML forms with Rails helpers

An oft unused attribute of the FORM element of HTML is the accept-charset. I'm a bit of a encoding fanatic and someone pointed out to me that I wasn't setting this attribute on a Rails application that I'd hacked up. As I'm always interested in learning something new about Rails, I decided to try setting this attribute in my application.

When using the base FormTagHelper#form_tag, it's pretty simple.

<% form_tag(search_solutions_path, {:'accept-charset' => "UTF-8"}) do -%>
...
<% end -%>

Note the use of the quotes around the symbol accept-charset. This is required because a hyphen requires escaping to be used in Ruby symbols.

When using the RESTful FormHelper#form_for, it's a bit more complicated and not very clearly documented. After some debugging and trial & error, I figured out that you have to add another level of indirection to add HTML options.

<% form_for(@model, {:html => {:'accept-charset' => "UTF-8"}}) do |f| %>
...
<% end -%>

0 comments: