I've got a rails app that displays a list of items called modules. I'm iterating over these, rendering a partial for each one that includes a remote_form_for call.
This all works, but fails HTML validation because my form text fields all have the same id.
Is there a :prefix option on the form (or something else) I can use to get around this?
Update:
(some code)
//_module_form.html.erb
<% remote_form_for app_module do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= submit_tag 'Save' %>
<%end %>
//parent page
<% @thing.modules.each do |app_module| %>
<%= render :partial => "module_form", :locals => { :app_module => app_module } %>
<% end %>
So if I have more than 1 item in the collection, I render the identical form on the same page, and the form id and textbox id are duplicated.
I can customize the form id pretty easily, but what about the text_box, since the controller is looking for specific named controls?
Add the :index => object.id
to the options hash when creating your form. This should generate ids in the form of object_id_attribute
without interfering with the controller.
Edit
The documentation regarding :index
is is ambiguously misleading. :index
does have an effect on the form submission. The solution is to specify the :id
attribute for each field. This will change the id
property, leaving name
unharmed (which is what counts for the submission).
Here is the code I use in a generic helper for generating these fields:
def create_field( f, field_type, object, field_name )
field_id = "#{object.class.name.downcase}_#{object.id.to_s}_#{field_name.to_s}"
f.send( field_type, field_name, :id => field_id )
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With