0

My ERB file works fine if I use text_field, but if I switch to text_field_tag I receive this error:

undefined method `text_field_tag' for #<ActionView::Helpers::FormBuilder:0x00000001f6fd50>

Here is the code that works:

<%= f.text_field mystring %>

And the code that does not work:

<%= f.text_field_tag mystring %>

text_field_tag is documented. How to make it work? Do I need a require or something?

Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
  • 1
    text_field_tag isnt a method of FormBuilder - its a view_helper like `link_to` so just use `text_field_tag` :) – krichard Jul 06 '11 at 08:12

2 Answers2

3

For your information, text_field_tag is from ActionView::Helpers::FormTagHelper, which states :

Provides a number of methods for creating form tags that doesn’t rely on an Active Record object assigned to the template like FormHelper does. Instead, you provide the names and values manually.

Since this is a helper that does not rely on an active record object, you cannot call this method for the "f" object. It's a a helper method that should be called like this :

<%= text_field_tag "whatever you want to write" %>
Dominic Goulet
  • 7,983
  • 7
  • 28
  • 56
2

Needed to remove f:

<%= text_field_tag mystring %>

I guess text_field_tag does not rely on the form_for.

Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
  • yea text_field belongs to the form_for, in the sense you need to use with an object, while text_field_tag is much more flexible. http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-text_field_tag and http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html – felix Jul 06 '11 at 12:09