I have two models, the first ( model_1) accepts nested attributes for the second ( model_2). The second model has only one field ( file), which is referred to in the form as a file field.
The problem occurs when the file is not selected. In this case - except with a text field - the field does not appear at all in the POST parameters, for which the first model believes that no nested model should be created at all. Which cannot initiate checks, etc. If I were to add a second field to model_2 and the corresponding form, and if I use text input, everything will go through precise and natural checks that work just fine for the file field.
Does anyone have any experience on how to do this?
And for the best some (simplified) code - the form:
= form_for @model_1, :html => { :multipart => true } do |f|
-
= f.fields_for :model_2 do |builder|
-
= builder.file_field :file
Model 1:
class Model1 < ActiveRecord::Base
has_many :model_2s, :dependent => :destroy
accepts_nested_attributes_for :model_2s
end
and Model 2:
class Model2 < ActiveRecord::Base
belongs_to :model_1
validates_presence:of :file
end
source
share