回形针不保存,没有错误

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

该项目中的另一个模型是为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 
= 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"=>#, "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模型? 你在使用@article.update_attributes(params[:article])吗?

因为如果你是因为它可能是由于错误使用attr_protectedattr_accessible 。 在这种情况下,您可以尝试分配文件

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