问题
Am stumbled - went through docs, tutorials, etc, and am not sure what I am doing wrong.
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
The model has the following line:
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
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)
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"}
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.
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.
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.
I noticed that I am also unable to remove the attachment when "remove_document" is checked. The db values for document_
* remains unchanged.
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.
回答1:
How are you updating the Article
model in the controller? Are you using @article.update_attributes(params[:article])
?
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]
来源:https://stackoverflow.com/questions/4221076/paperclip-not-saving-no-errors