Skip to content

Instantly share code, notes, and snippets.

@ronan-mch
Created December 18, 2014 21:55
Show Gist options
  • Save ronan-mch/775b28d81e643313c8c0 to your computer and use it in GitHub Desktop.
Save ronan-mch/775b28d81e643313c8c0 to your computer and use it in GitHub Desktop.
Custom SimpleForm input for use with multivalued Hydra models
class MultipleInput < SimpleForm::Inputs::Base
# Create one or more input fields for a multiple valued attribute
# including one empty input for adding new values.
def input(wrapper_options = {})
result = ''
# make sure the name is for a multi-valued parameter
input_html_options.merge!(name: "#{object.class.to_s.downcase}[#{attribute_name.to_s}][]")
if object.respond_to? attribute_name
value = object.send(attribute_name)
if value.is_a? Enumerable
# create an input for each existing value
value.each do |val|
input_html_options.merge!(value: val)
result += "#{@builder.text_field(attribute_name, input_html_options)}".html_safe
end
end
end
# Create a blank input for new values
input_html_options.merge!(value: '')
(result + "#{@builder.text_field(attribute_name, input_html_options)}").html_safe
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment