我被绊倒了 - 浏览了文档、教程等,但不确定我做错了什么。
项目中的另一个模型是为 Paperclip 设置的,并且在测试时可以正常工作。它将附件文件信息保存和检索到数据库中,并将文件放入 public/system 内的子文件夹中。我基本上将相关代码复制到我正在研究的模型中
该模型有以下行:
has_attached_file :document
链接到模型的表具有必要的列:
document_file_name
document_content_type
document_file_size
document_updated_at
编辑视图有这个(在 haml 中):
%h1 Knowledge Base: Edit Article
= message_block :on => @article
- form_for(@article, :url => knowledge_base_article_path(@article), :html => {:multipart => true}) do |f|
#knowledgebase.clearfix
%label Upload KB Document:
%br
= f.file_field :document
- if @article.document.exists?
%p
= link_to "Current KB Attachment", @article.document.url
%p
= f.check_box :remove_document
<br>
= render :partial => "form", :locals => {:f => f}
= submit_tag "Save changes"
= link_to "Cancel", knowledge_base_article_path(@article)
当我保存模型实例时,我可以在日志中看到 Rails 知道我正在尝试上传的文件:
Processing KnowledgeBase::ArticlesController#update (for 127.0.0.1 at 2010-11-18 19:21:01) [PUT]
Parameters: {"article"=>{"document"=>#<File:/var/folders/EZ/EZKwznNSGq4PAI4ll9NUD++++TI/-Tmp-/RackMultipart20101118-58632-19nvbc8-0>, "question"=>"Craig's Sandbox", "active"=>"0", "answer"=>"Nothing here, this is to test attachment functionality"}, "commit"=>"Save changes", "action"=>"update", "_method"=>"put", "authenticity_token"=>"MfH6RgLAQLnRBuf9WxgqWA+mIrDoBtYF+d4MW5DNCC0=", "id"=>"886", "controller"=>"knowledge_base/articles"}
然而,这四个的 db 值根本没有更新document_
* 列,它们保持 NULL。同一个表中的其他列更新得很好。
为了确保数据库列的命名正确,我将数据库列更改为其他内容,并在点击视图时出现错误,所以我知道数据库列的命名正确。
为了测试附件检索,我在 public/system 中手动创建了子文件夹(保存模型实例时附件将存放在其中),并且还手动修改了四个document_
* 表中的列。然后我转到上面的相同视图,它确实显示了相关的正确附件。
我注意到,当选中“remove_document”时,我也无法删除附件。的分贝值document_
* 保持不变。
就好像对这 4 列的读取操作有效,但写入操作不起作用(尽管如果我在编辑视图页面上修改模型实例中的某些内容,我可以让 Rails 修改同一个表中的其他列)。
有什么想法我可能在这里做错了吗?我确信我错过了一些明显的事情。