Created
December 18, 2014 21:55
-
-
Save ronan-mch/775b28d81e643313c8c0 to your computer and use it in GitHub Desktop.
Custom SimpleForm input for use with multivalued Hydra models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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