maschwenk
maschwenk

Reputation: 599

Simple-form associations creating empty input field

For some reason my simple_form code is inserting

<input name="order[product_ids][]" type="hidden" value="">

under the form

<label class="checkbox">
    <input class="check_boxes optional" id="order_product_ids_11" 
           name="order[product_ids][]" type="checkbox" value="11">Product11
</label>

When I use my has_and_belongs_to_many association form

<%= f.association :products, as: :check_boxes, include_blank: false %>  

with model Orders

class Order < ActiveRecord::Base
   belongs_to :user
   has_and_belongs_to_many :products

and products

class Product < ActiveRecord::Base
     has_and_belongs_to_many :orders

which causes the error:

ActiveRecord::RecordNotFound in OrdersController#create

Couldn't find all Products with IDs (3, 5, ) (found 2 results, but was looking for 3)


{"utf8"=>"✓",
 "authenticity_token"=>"F0c4J81QXRNMFDXrN55XrRafwj86lzUl3kXe/xXxKxc=",
 "order"=>{"order_type"=>"init_purchase",
 "shipping_status"=>"unshipped",
 "product_ids"=>["3",
 "5",
 ""]},
 "commit"=>"Create Order"}

because it passes an empty value for one of the products. When i manually remove the empty input field with no value, it does not have an issue.

Upvotes: 2

Views: 2323

Answers (1)

Vasiliy Ermolovich
Vasiliy Ermolovich

Reputation: 24617

It's not a SimpleForm's thing but Rails's. There will be a config option for this in Rails 4 but now you have to remove it manually.

You can read a bit about this here

Upvotes: 3

Related Questions