views:

746

answers:

2

I've been looking at the new options available in HTML5 forms, such as declaring input types as "email", "url", and "number", as described here.

How can I use these in conjunction with the rails form builders? I have tried

<% form_for @user do |f| %>
  <%= f.email :email, {:placeholder => '[email protected]'} %>
<% end %>

But that does not work. I've also tried

<% form_for @user do |f| %>
  <%= f.text_field :email, {:placeholder => '[email protected]', :type  => :email} %>
<% end %>

But the type is still "text" and not overridden. Is it possible, or is this something that will need to be addressed in Rails itself?

+5  A: 

Looks like there is currently an open ticket and patch for adding the HTML5 form input types. If you can't wait until the patch is accepted, you could apply the patch locally by freezing the actionpack gems and applying the patch or making an initializer that add the extra methods.

Of course the other option is adding the fields manually without a form helper:

<% form_for @user do |f| %>
  <%= tag(:input, {:type => :email, :value => f.object.email} %>
<% end %>
rnicholson
Thanks for the helpful reply. I added the field manually like you suggested, the code obviously needed a few more attributes e.g.<%= tag(:input, { :type => :email, :id => 'user_email', :name => 'user[email]', :placeholder => 'required', :value => f.object.email }) %>
the_snitch
A: 

Where'd you get the :placeholder from? I can't seem to find anything about it in the docs. I'm trying to get some input field placeholder text - i.e. grey text that says "Search..." but disappears when you click on it.

Tony M.
`placeholder` is an HTML5 attribute, it's part of HTML, not Rails, and it will only work on browsers that support it. http://www.jolora.co.uk/articles/html5-placeholder-attribute/
Tobias Cohen