Skip to content
This repository

Incorrect multiple select field in Rails 4 #905

danbob opened this Issue · 2 comments

3 participants

Dan Arnfield Anton Ageev Justin French
Dan Arnfield

Rails 4 has changed the way multiple select fields are generated. I have Document and Author classes in a habtm relationship. When I make a form for the Document class like this:

<%= semantic_form_for @document do |f| %>
    <%= f.inputs do %>
        <%= f.input :authors %>

It generates html like this:

<li class="select input optional" id="document_authors_input">
    <input name="document[author_ids][]" type="hidden" value="">
    <label class=" label" for="document_author_ids">Authors</label>
    <input name="document[author_ids][][]" type="hidden" value="">
    <select id="document_author_ids" multiple="multiple" name="document[author_ids][][]">

Rails adds its own hidden field, and more importantly, it adds its own "[]" to the field name after the one that Formtastic adds. This breaks things like @document.update_attributes(params[:document]) in the controller.

A workaround is to add these options to the input:

<%= semantic_form_for @document do |f| %>
    <%= f.inputs do %>
        <%= f.input :authors, include_hidden: false, input_html: { name: "document[author_ids]" } %>

All this also applies to models in has_many relationships.

Anton Ageev

I got same issue with rails from 3-2-stable branch.

Justin French

I've just tried this out on master, and I believe the [][] issues is already resolved:

<input name="post[section_id][]" type="hidden" value="" />
<label class="label" for="post_section_id">Section</label>
<input name="post[section_id][]" type="hidden" value="" />
<select id="post_section_id" multiple="multiple" name="post[section_id][]">
  <option value="1">Name</option>
  <option value="2">Name 2</option>

I've created a separate issue #945 for the multiple hidden fields (which I don't consider a blocker for a new Rails 4 compatible gem release) so that we can close this one off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.