开发者

form_tag garbles my text_field with HTML5 data attributes

I've got this code working in a separate view, but with inside of a form_for

<%= text_field :search_tags, :id => 'search_tags', 'data-autocomplete' => "lame sauce" %>

What I'm trying to 开发者_JAVA技巧do is create a simple form for form submission with autocomplete. It works great in the other location, but for some reason, as soon as I take this text_field out of form_for it generates this HTML:

<input id="search_tags_{:id=&gt;&quot;search_tags&quot;, &quot;data-autocomplete&quot;=&gt;&quot;lame sauce&quot;}" name="search_tags[{:id=&gt;&quot;search_tags&quot;, &quot;data-autocomplete&quot;=&gt;&quot;lame sauce&quot;}]" size="30" type="text">

It's all garbled and ugly :( Now, because this is a simple form I'm submitting I was just using form_tag, I just need it to go to a specific controller and action... that's it. Anyway, I've read through the text_field_tag and text_field docs, and it seems that neither does quite what i'd like it to do. What is a good solution to this problem? Or is there some way of using text_field or text_field_tag, that I'm not aware of? Many thanks!


If I understand, is only a problem in the way you used the text_field, try this:

<%= text_field :search, :tags, :id => 'search_tags', 'data-autocomplete' => "lame sauce" %>


Outside of form_for, you need to specify both an object name and then a method name as the first and second arguments if you want to use text_field and not text_field_tag.

In case you decide to go with text_field_tag (which is probably better for your purposes when you are outside of a form_for), make sure that you use the proper syntax: the first argument is the id, and the rest is the options hash.

References: RoR API: FormHelper#text_field, RoR API

P.S. You don't really need to override the id like that -- it defeats part of the purpose of form_for, which generates RESTful-style forms.


First parameter is the id, second parameter is the value. Needs to look something like this:

<%= text_field_tag "search_tags", '', 'data-autocomplete' => "lame sauce" %>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜