0
我有一個表單,我創建了一個項目。項目可能有很多附件。 附件安裝了一個DocumentUploader。 但是當我提交表單時,上傳的文件沒有附加到我創建的附件中。任何想法可能是錯誤的?當我從控制檯進行測試時,我可以將文檔添加到附件中。Rails CarrierWave:上傳的文件沒有保存
形式
66 .field
67 = f.label :attachment, 'Velg vedlegg'
68 = file_field_tag "attachments[]", type: :file, multiple: true
控制器
53 if @project.update(project_params)
54
55 if params[:attachments]
56 params[:attachments].each do |attachment|
57 a = @project.attachments.create!(document: attachment)
58 Rails.logger.debug "attachment as seen from the params hash: #{attachment}"
59 Rails.logger.debug "attachment.document is #{a.document.inspect}"
60 Rails.logger.debug "URL is nil #{a.document.url == nil}"
從日誌
web.1 | Processing by Customers::ProjectsController#update as HTML
web.1 | Parameters: {"utf8"=>"✓", "authenticity_token"=>"XX=",
"project"=>{"name"=>"", "description"=>"",
"attachments"=>["Screen Shot 2014-04-13 at 21.18.34.png"],
web.1 | attachment as seen from the params hash: Screen Shot 2014-04-13 at 21.18.34.png
web.1 | attachment.document is #<DocumentUploader:0x007fad727003d8
@model=#<Attachment id: 17, document: nil, project_id: 4,
created_at: "2014-08-27 11:42:00", updated_at: "2014-08-27 11:42:00">,
@mounted_as=:document, @storage=#<CarrierWave::Storage::File:0x007fad761cef28
@uploader=#<DocumentUploader:0x007fad727003d8 ...>>>
web.1 | URL is nil true
project.rb
class Project < ActiveRecord::Base
has_many :attachments
attachment.rb
1 class Attachment < ActiveRecord::Base
2 mount_uploader :document, DocumentUploader
3 end
具有更好的錯誤檢測
>> a
=> #<Attachment id: 21, document: nil, project_id: 4, created_at: "2014-08-27 12:57:42", updated_at: "2014-08-27 12:57:42">
>> a.document = attachment
=> "400.png"
>> a.save
=> true
>> a
=> #<Attachment id: 21, document: nil, project_id: 4, created_at: "2014-08-27 12:57:42", updated_at: "2014-08-27 12:58:10">
>> a.document.url
=> nil
如果您檢查文檔上的附件,您是否看到文檔屬性已填充?我想知道是不是因爲你正在查看文檔的模型實例變量與它自己的文檔。 – agmcleod 2014-08-27 12:11:30
我不確定你的意思。你可以試着重新措辭嗎? :) – martins 2014-08-27 12:45:01
當然。如果你登錄'Rails.logger.debug「>>>>>>>>>#{a.inspect}」'你看到填充文件名的文檔字段嗎? – agmcleod 2014-08-27 12:49:17