回形针不保存,没有错误 [英] Paperclip not saving, no errors

查看:42
本文介绍了回形针不保存,没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被绊倒了 - 浏览了文档、教程等,但不确定我做错了什么.

Am stumbled - went through docs, tutorials, etc, and am not sure what I am doing wrong.

项目中的另一个模型是为 Paperclip 设置的,并且在测试时可以正常工作.它将附件文件信息保存并检索到数据库中,并将文件放入 public/system 内的子文件夹中.我基本上将相关代码复制到我正在研究的模型中

Another model in the project is set up for Paperclip and is functional when tested. It saves and retrieves attachment file info into the database, and puts file into a subfolder inside public/system. I basically copied the relevant code over toward the model I am working on

模型有以下几行:

has_attached_file :document

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

The table, which model is linked to, has necessary columns:

document_file_name 
document_content_type
document_file_size
document_updated_at

编辑视图有这个(在haml中):

The edit view has this (in 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 知道我要上传的文件:

When I save the model instance, I can see in log that Rails is aware of the file I am trying to upload:

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"}

然而,四个 document_* 列的 db 值根本没有更新,它们保持为 NULL.同一个表中的其他列更新得很好.

Yet the db values are not updated at all for the four document_* columns, they remain NULL. The other columns in same table are updated fine.

为了确保 db 列的命名正确,我将 db 列更改为其他内容并在点击视图时出错,所以我知道 db 列的命名正确.

Just to be sure the db columns are named correctly, I changed db columns to something else and got error upon hitting the view, so I know the db columns are named correctly.

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

To test attachment retrieval, I manually created subfolders inside public/system (where the attachment would have went when model instance is saved), and also manually modified the four document_* columns in the table. I then went to the same view above, and it did show the correct attachment in question.

我注意到在选中remove_document"时我也无法删除附件.document_* 的 db 值保持不变.

I noticed that I am also unable to remove the attachment when "remove_document" is checked. The db values for document_* remains unchanged.

就好像对这 4 列的读取操作有效,但写入操作无效(尽管如果我在编辑视图页面上修改模型实例中的某些内容,我可以让 Rails 修改同一个表中的其他列).

It is as if read operation on those 4 columns work, but the write operation does not (though I am able to have Rails modify other columns in the same table if I modify something in the model instance on the edit view page).

知道我在这里可能做错了什么吗?我确定我错过了一些明显的东西.

Any ideas what I could be doing wrong here? I am sure I missed something obvious.

推荐答案

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

How are you updating the Article model in the controller? Are you using @article.update_attributes(params[:article]) ?

因为如果你是,那么它可能是由于 attr_protectedattr_accessible 的错误使用造成的.在这种情况下,您可以尝试使用

Cause if you are then it might be caused by incorrect use of attr_protected or attr_accessible. In that case you can try to assign the file with

@article.document = params[:article][:document]

这篇关于回形针不保存,没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆