Rails: undefined method text_field_tag

僤鯓⒐⒋嵵緔 提交于 2019-12-13 03:44:31

问题


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?


回答1:


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



回答2:


Needed to remove f:

<%= text_field_tag mystring %>

I guess text_field_tag does not rely on the form_for.



来源:https://stackoverflow.com/questions/6593263/rails-undefined-method-text-field-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!