Skip to content

Instantly share code, notes, and snippets.

@ches
Created August 26, 2011 08:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ches/1172956 to your computer and use it in GitHub Desktop.
Save ches/1172956 to your computer and use it in GitHub Desktop.
SimpleForm (1.4.x) input for mongoid_taggable -- place in app/inputs
# Input builder to render tags from an Array field, for use with my fork
# of mongoid_taggable:
#
# https://github.com/ches/mongoid_taggable
#
class TagsInput < SimpleForm::Inputs::StringInput
map_type :tags, :to => :text_field
protected
def html_options_for(namespace, extra)
formatted_tags = object.send(attribute_name).
try(:join, "#{object.send(:tags_separator)} ")
super.merge(:value => formatted_tags)
end
end
@tmaier
Copy link

tmaier commented Oct 10, 2011

Using simple_form (1.5.2) and this snippet, I get the following error message:

     ActionView::Template::Error:
       undefined method `map_type' for TagsInput:Class

when I remove map_type, I need to add

f.input :tags, as: :tags

The html created looks like this:

<div class="input string tags optional" value="Tag1, Tag2, Tag3">
  <label class="tags optional" for="work_breakdown_structure_tags_tag1_tag2_tag3"> Tags</label>
  <input class="string tags optional" id="work_breakdown_structure_tags" name="work_breakdown_structure[tags]" size="50" type="tags" value="Tag1, Tag2, Tag3">
</div>

This is malformed html because of the value attribute in the div tag and the wrong for attribute in the label tag

Any other idea how to set the input.value attribute?

@ches
Copy link
Author

ches commented Oct 10, 2011

Sorry, this was written for SimpleForm 1.4.x. I haven't updated yet, so I'm not sure how the API has changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment