微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – Paperclip不保存,没有错误

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

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

该模型具有以下行:

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

但是,对于四个document_ *列,db值根本不会更新,它们仍为NULL.同一表中的其他列更新正常.

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

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

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

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

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

解决方法

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐