1

In a Rails application, I came across something like this (in a haml file):

= form_for(resource, url:<url>, ...) do |f|
  = f.email :email, required: true, id: 'registration_email" ...

Looking at the docs, I can find an email_field method, but not an email method. I have dug into the source as well, but so far no dice. I appreciate any insight!

Morgan Laco
  • 160
  • 2
  • 10

2 Answers2

2

It turns out the email method I was referring to was in a custom builder that was being passed into the form_for method.

Morgan Laco
  • 160
  • 2
  • 10
1

We use email_field with and without a form builder and specify your resource inside it, like in example posted in docs

email_field("user", "email")
# => <input id="user_email" name="user[email]" type="email" />

or

= form_for(resource, url:<url>, ...) do |f|
  = f.email_field :email, required: true, id: 'registration_email" ... 

Which will generate

<input id="resource_email" name="resource[email]" type="email" />

I don't think there's a .email helper in general because i just tested and it gave me syntax error. Needs to check but it could be that the gem you are using may have defined it for you otherwise i don't think it'll work

Mandeep
  • 9,093
  • 2
  • 26
  • 36
  • Thank you for your response. I am wondering though, where is the FormHelper method `email` defined?[...time passes and I look at the source...] Wait, I get it now! The methods defined in the source are all `*_field`, but you always just use `f.*` to use them! So somewhere, there must be a `define_method` that appends `_method` to the type passed in (e.g. `email`,`text`, `phone`) to produce the field. – Morgan Laco Aug 12 '14 at 17:26
  • @MorganLaco Yeah on the basis only i wrote my answer :) but just tested it and it doesn't seems to be working for me. Can you show me where you saw it? Also you might want to unaccept my answer :) – Mandeep Aug 12 '14 at 17:42
  • If you look [here](https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/form_helper.rb#L776) for instance, you can see that there is a `text_field` method in the helper, but to create a text field, you use `f.text`, not `f.text_field`. – Morgan Laco Aug 12 '14 at 19:10
  • @MorganLaco i know that's why i wrote my answer thinking about that. Can you give me a link where you saw .email helper used? – Mandeep Aug 13 '14 at 06:32