简体   繁体   中英

How can I use token fields with acts_as_taggable_on and formtastic?

I would like to be able to use the token fields (found here: http://railscasts.com/episodes/258-token-fields ) with formtastic to allow users to input tags (I am using acts_as_taggable_on).

Can someone walk me through an example of how to do this?

I don't have the time for a full walk-through, but the guts of this seems to be that a text_field has a data-pre attribute with some JSON in it. The way to add attributes to inputs in Formtastic is via the:input_html option, so this:

<p>
  <%= f.label :author_tokens, "Authors" %><br />
  <%= f.text_field :author_tokens, "data-pre" => @book.authors.map(&:attributes).to_json %>
</p>

Might be translated to something like:

<%= f.input :author_tokens, :input_html => { "data-pre" => @book.authors.map(&:attributes).to_json } %>

The rest is gluing CSS to HTML and finding the right DOM element to apply the jQuery to (view source is your friend).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM