I used a fake input field to get simple_form to render a field that doesn't correspond to a field in the model.
class FakeInput < SimpleForm::Inputs::StringInput
def input(wrapper_options = nil)
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
template.text_field_tag(attribute_name, nil, merged_input_options)
end
end
In my form, the model I am working with is deeply nested. This means the input name is long and very specific. An Event
has_many
Positions
which has_many
Responsibilities
. I want to add a fake field to the responsibilities called position_count
.
As you can see in the markup below, the label gets the correct value for its for
attribute but the input does not get the correct name
attribute. What do I need to change (possibly in the fake_input.rb
class?) to get the input to have the correct name?
The correct name for the input should be
event_positions_attributes_0_responsibilities_attributes_0_position_count
but the fake field is only rendered as
position_count
The markup being rendered is...
<div class="input fake optional event_positions_responsibilities_position_count">
<label for="event_positions_attributes_0_responsibilities_attributes_0_position_count" class="fake optional control-label">Position count</label>
<input type="text" name="position_count" id="position_count" class="fake optional">
</div>
input_html: { name: 'event_positions_attributes_0_responsibilities_attributes_0_position_count' }
– Petr Gazarovattr_accessor :position_count
toResponsibility
and you can just create an inputs as if it was a regular database column backed attribute. – max