[英]Paperclip saving files to filesystem but not to model / db
可以肯定的是,我的問題是由某些錯誤引起的,該錯誤使我無法僅使用帶有主要attr_accesible: cover
回形針。
我收到此錯誤:
Issue model missing required attr_accessor for 'cover_file_name'
因此,這可能是由於mass_assignment的安全性更改引起的回形針錯誤?
我有一個帶有回形針3(Gemfile.lock中的3.4.2)的新Rails項目(3.2.13)。 我正在嘗試在問題模型中通過回形針上傳文件。 它們保存到文件系統,但不保存到對象或數據庫。
我已經嘗試過嘗試保存這些方法的所有組合(我認為)。
issues_controller.rb
def create
@issue = Issue.new
@issue.attributes = params[:issue]
respond_to do |format|
if @issue.save
format.html { redirect_to @issue, notice: 'Issue was successfully created.' }
format.json { render json: @issue, status: :created, location: @issue }
else
format.html { render action: "new" }
format.json { render json: @issue.errors, status: :unprocessable_entity }
end
end
end
形成:
<%= form_for @issue, :multipart => true, :method => :post do |f| %>
....
<%= f.file_field :cover %>
模型:
class Issue < ActiveRecord::Base
has_many :pages
attr_accessible :number, :name, :cover
has_attached_file :cover, :styles => { :medium => "300x300>"}, :default_url => "/images/:style/missing.png"
attr_accessor :cover_file_name, :cover_content_type, :cover_file_size, :cover_updated_at
validates_attachment :cover, :presence => true
end
我想我已經查看了關於Stackoverflow的所有其他回形針問題建議。 ImageMagick正在工作並且是最新的。 我沒有保存任何錯誤,文件正確顯示在文件系統中。 我的debug語句輸出顯示文件名並顯示:
/system/issues/covers//original/image_name.jpg?1377891456
[paperclip] Saving attachments.
但還會顯示數據庫值的空值:
SQL (1.4ms) INSERT INTO `issues` (`cover_image_content_type`, `cover_image_file_name`, `cover_image_file_size`, `cover_image_updated_at`, `created_at`, `name`, `number`, `updated_at`) VALUES (NULL, NULL, NULL, NULL, '2013-08-30 19:49:54', 'Test', 'JPEG 30', '2013-08-30 19:49:54')
有什么想法嗎? 有什么建議嗎? TIA。
因此,這的最終原因是數據庫中列的名稱與模型中使用的名稱不同。 不知何故,我在遷移中有cover_image,而在模型中有Cover。 故事的寓意,如果您對此感到困惑,請確保首先檢查列名 。
檢查您的params哈希值,但是我認為您將從表格中獲得的只是cover
。 因此使用:
attr_accessible :cover
而不是所有列名。 回形針在內部設置這些屬性,而不是通過批量分配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.