开发者

Type attribute for text_field

I have this code:

<%= f.text_field :email, :type => "email", :placeholder => "john@example.com" %>

So people can enter their email on an iPhone with the email keyboard instead of the开发者_StackOverflow社区 ASCII keyboard. However, the output is:

<input id="user_email" name="user[email]" placeholder="john@example.com" size="30" type="text" />

which should be:

<input id="user_email" name="user[email]" placeholder="john@example.com" size="30" type="email" />

I also want this for the tel type, so people get the telephone number keyboard.

Is there a way to force Rails to use the email type instead of text, or must I use HTML directly? Thanks


It is not possible to do this with the current helpers (http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html) "Returns an input tag of the "text" type tailored for accessing a specified attribute..."

I'd write a helper to keep your code nicer. You could just write something with the HTML and options you need, or you could add something to work with FormBuilder like the snippet below.

module ActionView
  module Helpers
    class FormBuilder
      def tel_field(method, options = {})
        InstanceTag.new(@object_name, method, self, options.delete(:object)).to_input_field_tag("tel", options)
      end
    end
  end
end


Actually, in Rails 3 there are form helpers for this purpose: telephone_field and email_field work just like text_field, but set their type attributes as you would expect..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜