rails nested form changes id to decimal

2019-09-12 12:03发布

问题:

Quick problem description

The value of the id in a nested form is rendered as 10006.0 instead of 10006 (decimal/float instead of an integer) resulting in an ActiveRecord::RecordNotFound exception.

What is causing this behaviour??

The code setup

So I followed the rails-guides tutorial on how to build nested forms.

My 2 models are:

class Broker < ActiveRecord::Base
  has_many :execution_groups, dependent: :destroy
  accepts_nested_attributes_for :execution_groups, 
                                allow_destroy: true, 
                                reject_if: lambda{|att| att['port'].blank?}
end

and

class ExecutionGroup < ActiveRecord::Base
  belongs_to :broker
  validates :port, presence: true, numericality: true
end

My form includes (haml snippet) with the :id field explicitly rendered for debugging purposes.

= form_for( @broker ) do |f|
  ...
  = f.fields_for :execution_groups, include_id: false do |eg_form|
    %tr
      %td=eg_form.text_field :name, class: 'form-control', type: 'text'
      %td=eg_form.text_field :port, class: 'form-control', type: 'number'
      %td
        =eg_form.check_box :_destroy
        =eg_form.text_field :id, class: 'form-control'

The value of the id box is rendered as 10006.0 instead of 10006 (decimal/float instead of an integer) resulting in an ActiveRecord::RecordNotFound exception when updating, because there's only a record with id 10006.

My current environment

Otherwise fully functioning

  • jruby 1.7.19 (1.9.3p551) 2015-01-29 20786bd on Java HotSpot(TM) 64-Bit Server VM 1.8.0_45-b14 +jit [darwin-x86_64]
  • JDBC Oracle Thin Driver, Implementation-Version: 11.2.0.4.0
  • Rails 4.0.13
  • Boostrap 3 for Layouts