我正在尝试从rails中的嵌套表单创建一个对象,但我似乎无法让params看起来像他们应该的那样。
我有Submission
和Attachment
型号
class Attachment < ApplicationRecord
mount_uploader :file, FileUploader
belongs_to :submission
end
class Submission < ApplicationRecord
has_many :attachments, inverse_of: :submission
accepts_nested_attributes_for :attachments, reject_if: :all_blank, allow_destroy: true
end
def new
@submission = ComplianceSubmission.new(user_id: current_user.id)
@submission.attachments.build
end
形成
<%= form.fields_for :attachments, submission.attachments do |attachment_fields| %>
<%= attachment_fields.file_field :file %>
<% end %>
<ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"tY5gRq1YJatO/wHHltCtBr34ZpgIAofJBCNcStiWUrAh0NavrAvHNjhesqMl0aRct9wlBQacpnaietb654JtsA==", "compliance_submission"=><ActionController::Parameters {"compliance_selection"=>"Professional Designation", "attachments"=>{"file", "compliance_conversation"=>"", "compliance_conversation_user"=>"Grady Ledner", "compliance_conversation_time"=>"Nov 10, 4:44 PM", "compliance_note"=>""} permitted: false>, "commit"=>"Submit Form", "controller"=>"compliance_submissions", "action"=>"create"} permitted: false>
嵌套属性参数像"attachments"
一样转到控制器,它们看起来像"attachments_attributes"
。另外,系统不知道创建模型。
有人知道我做错了吗?