Rails:未定义的方法text_field_tag

Rails:未定义的方法text_field_tag

问题描述:

如果我使用 text_field ,我的ERB文件工作正常,但是如果切换到 text_field_tag ,我会收到此错误:

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>

这是有效的代码:

<%= f.text_field mystring %>

无效的代码:

<%= f.text_field_tag mystring %>

text_field_tag 。如何使其运作?我需要需求或其他东西吗?

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

text_field_tag 来自 ActionView :: Helpers :: FormTagHelper ,其中指出:



提供了多种创建表单标签的方法,这些标签不依赖
分配给
的Active Record对象,该模板就像FormHelper一样。
而是您手动提供名称和
值。

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.

因为这是一个不依赖的助手在活动记录对象上,不能为 f对象调用此方法。这是一个应该这样的帮助方法:

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" %>