CarrierWave。无法使用STI保存文件

时间:2012-03-19 07:43:15

标签: ruby-on-rails carrierwave

我有模型Document

class Document < ActiveRecord::Base
  belongs_to :company
  validates :name, :presence => true
end

继承自Document的两个类:

License

class License < Document
  mount_uploader :file, DocumentUploader
end

Certificate

class Certificate < Document
  mount_uploader :file, DocumentUploader
end

当我尝试对证书执行current_company.licenses.create(...)或相同操作时,除了file之外,始终保存所有参数,nil

此外,我已尝试在file模型中安装Document ...请帮帮我。

这是日志:

Started POST "/companies/1/verified" for 127.0.0.1 at Mon Mar 19 09:33:41 +0200 2012
Processing by CompaniesController#verified as HTML
  Parameters: {"verified"=>{"certificate"=>{"name"=>"Certificate", "file"=>"test.png"}, "insured"=>"2000000", "suppliers"=>"", "license"=>{"name"=>"License", "file"=>"test.png"}}, "authenticity_token"=>"0hIn41Tjonm/AXZBKM1PE/tjQxJDLqZaojMTHDoZq2k=", "id"=>"1", "utf8"=>"✓", "commit"=>"Update verifications"}
  Company Load (0.7ms)  SELECT "companies".* FROM "companies" WHERE "companies"."id" = 1 LIMIT 1
   (0.1ms)  BEGIN
  SQL (0.8ms)  INSERT INTO "documents" ("company_id", "created_at", "file", "name", "type", "updated_at", "verified") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["company_id", 1], ["created_at", Mon, 19 Mar 2012 07:33:41 UTC +00:00], ["file", nil], ["name", "License"], ["type", "License"], ["updated_at", Mon, 19 Mar 2012 07:33:41 UTC +00:00], ["verified", false]]
   (0.6ms)  COMMIT
   (0.1ms)  BEGIN
  SQL (0.5ms)  INSERT INTO "documents" ("company_id", "created_at", "file", "name", "type", "updated_at", "verified") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["company_id", 1], ["created_at", Mon, 19 Mar 2012 07:33:41 UTC +00:00], ["file", nil], ["name", "Certificate"], ["type", "Certificate"], ["updated_at", Mon, 19 Mar 2012 07:33:41 UTC +00:00], ["verified", false]]
   (0.4ms)  COMMIT
Redirected to http://localhost:3000/company/profile
Completed 302 Found in 18ms (ActiveRecord: 3.3ms)

感谢。

1 个答案:

答案 0 :(得分:1)

您确定已经设置了上传文件的表单以获得多部分有效负载吗?

如果不是该文件在提交时不会被发送,则rails将仅接收文本表单数据。在form_helper标记内,您需要添加。

:html => {:multipart => true}