Question

I have the following select box that was created using nested form:

<select name="product[shop_attributes][id]" id="product_shop_attributes_id">
  <option value="23">KMART</option>
  <option value="24">Super Shop</option>
  <option selected="selected" value="22">TARGET</option>
  <option value="new">Create New Shop</option>
</select>

selected="selected" was created by passing :selected => "22" to f.select options.

The problem is that no matter what option is selected, the submitted value is always "22".

I noticed that a hidden input is created, which I believe causes the problem:

<input type="hidden" value="22" name="product[shop_attributes][id]" id="product_shop_attributes_id">

Thus, there are 2 elements with id=product_shop_attributes_id.

What could cause to this hidden input field to be generated ?


Relevant code of select box creation:

<%= form_for @product do |f| %>
  <%= f.fields_for :shop do |sf| %>
    sf.select(:id, <options>, {:prompt => true, :selected => <default_value>})
  <% end %>
<% end %>

Relevant controller code:

def edit
  @product = Product.find(params[:id]) # the select box indeed gets it's initial value from @product
end

def update
  @temp = params.inspect
end

update.html.erb:

<%= @temp %>

I see here always the same (no matter what option is selected):

"product"=>{"shop_attributes"=>{"id"=>"22"},...}
Was it helpful?

Solution 2

The problem is, as I mentioned in the question, the hidden input field with the same id as the select.

I opened a separate question to investigate why this happens.

OTHER TIPS

There's nothing wrong with the rails generated HTML. It's probably the way you're accessing it in your controller. Could you post the original rails code that generated this HTML and the code you are using to process it?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top