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] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |