I am implementing nested forms in Rails 4 using cocoon. I am trying to update the model with nested attributes for other model.
I have request model with nested attributes for filled cartridges:
has_many :filled_cartridges, dependent: :destroy
accepts_nested_attributes_for :filled_cartridges, allow_destroy: true
In my update method for request_controller.rb i have:
.. some code..
@request.update(request_params)
.. some other code ..
Where request_params are defined:
def request_params
params.require(:request).permit(:name, :address,:description,
:filled_cartridges_attributes => [:cartridge_name,:client_id,:cartridge_id, :request_id,:count,:fill_date,:_destroy,:id],
end
The model filled_cartridges has columns:
t.integer "client_id", null: false
t.string "cartridge_name", null: false
t.integer "cartridge_id", null: false
In my nested form I can delete a field update or add new field as in Railscasts tutorial. When I update one of the field it works, when i delete one of fields it works too. I get an error when try to add new field with some value and hit submit:
SQLite3::ConstraintException: NOT NULL constraint failed: filled_cartridges.client_id:
So it tries to handle nested model too. How to make that the nested models will not be handled at the first time, i need them later, but not at the moment I update my request model.
Note: If I delete my nested strong parameters it will work fine for add and update fields, but it WON'T work for destroying filed, as it requires nested params _destroy and id to be specified.