2010-11-19 46 views
4

我偶然發現 - 經歷了文檔,教程等,我不知道我做錯了什麼。回形針沒有保存,沒有錯誤

該項目中的另一個模型設置爲回形針,並且在測試時功能正常。它將附件文件信息保存並檢索到數據庫中,並將文件放入公共/系統內的子文件夾中。我基本上在複製的相關代碼向我的

工作模型具有以下行的模式:

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_ *列,他們仍然NULL。同一個表中的其他列更新正常。

爲了確保db列的命名正確,我將db列更改爲其他值,並在點擊視圖時出錯,所以我知道db列命名正確。

要測試附件檢索,我手動創建了公共/系統內的子文件夾(在保存模型實例時附件會去掉),並且還手動修改了表格中的四個document_ *列。然後,我採用了上述相同的觀點,並確實顯示了正確的依戀。

我注意到,當「remove_document」被選中時,我也無法刪除附件。 document_ *的db值保持不變。

這就好像對這4列的讀操作起作用,但是寫操作不起作用(儘管如果我在編輯視圖頁面的模型實例中修改了某些內容,我可以讓Rails修改同一個表中的其他列)。

任何想法,我可能做錯了嗎?我相信我錯過了一些明顯的東西。

回答

8

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

原因如果你是那麼它可能是由於不正確使用attr_protectedattr_accessible引起的。在這種情況下,您可以嘗試分配文件

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

謝謝。罪魁禍首確實在控制器中,現在已經修復。我有params [:some_namespace_before_article]而不是params [:article],現在已解決!標記爲已解決。 – 2010-11-19 01:31:11

相關問題