Rails 3-通过表单创建的嵌套对象,但没有保存属性

时间:2014-03-13 16:22:01

标签: ruby-on-rails ruby-on-rails-3 nested-attributes

我发现了类似的问题,但没有一个在这个具体的例子中有所帮助。我有一个验证模型和一个上传模型。当用户创建验证时,他们也可以向其添加上传。我的验证创建得很好,并创建了一个上传(但只有一个,我需要能够添加许多上传)。但是,上传的每个属性都保存为nil。我可以通过自己的表单创建上传文件,但不是嵌套表单。

表单视图

<%= nested_form_for @verification do |f| %>

  ...

      <%= f.fields_for :uploads do |upload_form| %>
        <%= upload_form.label :select_file %>
        <%= upload_form.file_field :data %>
        <%= upload_form.link_to_remove "Remove this file" %>
      <% end %>
      <p><%= f.link_to_add "Add a file", :uploads %></p>
<% end %>

<%= f.submit %>

<% end %>

verification.rb

class Verification < ActiveRecord::Base
  attr_accessible :uploads_attributes

  has_many :uploads, autosave: true
  accepts_nested_attributes_for :uploads, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true

end

verifications_controller.rb

class VerificationsController < ApplicationController

    def new
        @verification = Verification.new(params[:verification])
        @uploads = Upload.new(params[:uploads_attributes])
     end

    def create
        @verification = Verification.new(params[:verification])
        @verification.save
        @verification.uploads.create
    end

输出:

Started POST "/verifications" for 127.0.0.1 at 2014-03-13 11:00:04 -0500
Processing by VerificationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"3an81f1MRLGXexkBA9hcBuRdAdIb1XNWLhPRC60FSa0=", "verification"=>{"project_id"=>"1", "checked_on"=>"", "notes"=>"", "uploads_attributes"=>{"1394725257840"=>{"data"=>#<ActionDispatch::Http::UploadedFile:0x445f320 @original_filename="test.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"verification[uploads_attributes][1394725257840][data]\"; filename=\"test.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:C:/Users/kctsrk/AppData/Local/Temp/RackMultipart20140313-2704-bs31i7>>, "_destroy"=>"false"}}, "compliance"=>"Compliant"}, "commit"=>"Create Verification"}
  [1m[35mAccount Load (1.0ms)[0m  SELECT "accounts".* FROM "accounts" WHERE "accounts"."remember_token" = '2-4ARijYLtx4uSpV2A7oWA' LIMIT 1
  [1m[36mProject Load (0.0ms)[0m  [1mSELECT "projects".* FROM "projects" WHERE "projects"."id" = 1 LIMIT 1[0m
  [1m[35m (0.0ms)[0m  begin transaction
  [1m[36mProject Exists (0.0ms)[0m  [1mSELECT 1 AS one FROM "projects" WHERE "projects"."id" = 1 LIMIT 1[0m
  [1m[35mSQL (1.0ms)[0m  INSERT INTO "verifications" ("account_id", "approved", "attachment", "attachment2", "checked_on", "compliance", "created_at", "dateimplemented", "notes", "project_id", "project_name", "state_id", "submitted_on", "updated_at", "verifier_name") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["account_id", 1], ["approved", true], ["attachment", nil], ["attachment2", nil], ["checked_on", nil], ["compliance", "Compliant"], ["created_at", Thu, 13 Mar 2014 16:00:04 UTC +00:00], ["dateimplemented", 2014-03-13 11:00:04 -0500], ["notes", ""], ["project_id", 1], ["project_name", nil], ["state_id", 4], ["submitted_on", nil], ["updated_at", Thu, 13 Mar 2014 16:00:04 UTC +00:00], ["verifier_name", nil]]
  [1m[36m (88.0ms)[0m  [1mcommit transaction[0m
  [1m[35m (0.0ms)[0m  begin transaction
  [1m[36mSQL (3.0ms)[0m  [1mINSERT INTO "uploads" ("created_at", "data", "filename", "mime_type", "name", "updated_at", "verification_id") VALUES (?, ?, ?, ?, ?, ?, ?)[0m  [["created_at", Thu, 13 Mar 2014 16:00:04 UTC +00:00], ["data", nil], ["filename", nil], ["mime_type", nil], ["name", nil], ["updated_at", Thu, 13 Mar 2014 16:00:04 UTC +00:00], ["verification_id", 38]]
  [1m[35m (86.0ms)[0m  commit transaction
  [1m[36m (0.0ms)[0m  [1mbegin transaction[0m
  [1m[35mProject Exists (1.0ms)[0m  SELECT 1 AS one FROM "projects" WHERE "projects"."id" = 1 LIMIT 1
  [1m[36m (0.0ms)[0m  [1mcommit transaction[0m
Redirected to http://localhost:3000/verifications/38
Completed 302 Found in 417ms (ActiveRecord: 184.0ms)

所以它传递了uploads_attributes,但实际上没有使用它们来创建上传对象。

0 个答案:

没有答案