回形针不保存,没有错误

Cra*_*gan 4 ruby ruby-on-rails paperclip

我跌跌撞撞 - 经历了文档,教程等,我不确定我做错了什么.

该项目中的另一个模型是为Paperclip设置的,并在测试时起作用.它将附件文件信息保存并检索到数据库中,并将文件放入公共/系统内的子文件夹中.我基本上将相关代码复制到我正在处理的模型上

该模型具有以下行:

has_attached_file :document
Run Code Online (Sandbox Code Playgroud)

该模型链接到的表具有必要的列:

document_file_name 
document_content_type
document_file_size
document_updated_at
Run Code Online (Sandbox Code Playgroud)

编辑视图有这个(以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)
Run Code Online (Sandbox Code Playgroud)

当我保存模型实例时,我可以在日志中看到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"}
Run Code Online (Sandbox Code Playgroud)

然而,对于四个document_*列,db值根本不更新,它们保持为NULL.同一表中的其他列更新正常.

为了确保db列被正确命名,我将db列更改为其他内容并在访问视图时出错,因此我知道db列已正确命名.

为了测试附件检索,我手动创建了public/system中的子文件夹(保存模型实例时附件会去的地方),并且还手动修改了document_表中的四个*列.然后我转到上面的相同视图,它确实显示了正确的附件.

我注意到,当检查"remove_document"时,我也无法删除附件.document_*的db值保持不变.

就好像这4列的读操作一样,但写操作没有(虽然我可以让Rails修改同一个表中的其他列,如果我修改了编辑视图页面上的模型实例中的内容).

我在这里做错了什么想法?我相信我错过了一些明显的东西.

Dan*_*nne 8

你是如何Article在控制器中更新模型的?你在用@article.update_attributes(params[:article])吗?

因为如果你是因为它可能是由于错误使用attr_protected或造成的attr_accessible.在这种情况下,您可以尝试分配文件

@article.document = params[:article][:document]
Run Code Online (Sandbox Code Playgroud)