简体   繁体   English

回形针将文件保存到文件系统,但不保存到模型/数据库

[英]Paperclip saving files to filesystem but not to model / db

update 更新

Pretty sure my problem is caused by something wrong that isn't allowing me to just use paperclip with the main attr_accesible: cover in my issue.rb model. 可以肯定的是,我的问题是由某些错误引起的,该错误使我无法仅使用带有主要attr_accesible: cover回形针。

I get this error: 我收到此错误:

Issue model missing required attr_accessor for 'cover_file_name'

So this is some sort of paperclip bug caused by security changes to mass_assignment maybe?? 因此,这可能是由于mass_assignment的安全性更改引起的回形针错误?

Original Problem 原始问题

I have a new rails project (3.2.13) with Paperclip 3 (3.4.2 in Gemfile.lock). 我有一个带有回形针3(Gemfile.lock中的3.4.2)的新Rails项目(3.2.13)。 I'm trying to upload files via paperclip in an issues model. 我正在尝试在问题模型中通过回形针上传文件。 They save to the file system, but not to the object or database. 它们保存到文件系统,但不保存到对象或数据库。

I've tried every combination (I think) of trying to save these. 我已经尝试过尝试保存这些方法的所有组合(我认为)。

Relevant code: 相关代码:

issues_controller.rb 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: 形成:

<%= form_for @issue, :multipart => true, :method => :post do |f|  %>
....
<%= f.file_field :cover %>

Model: 模型:

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

I think I've looked through all other paperclip problem suggestions on Stackoverflow. 我想我已经查看了关于Stackoverflow的所有其他回形针问题建议。 ImageMagick is working and up to date. ImageMagick正在工作并且是最新的。 I don't get any errors saving and files show up correctly in file system. 我没有保存任何错误,文件正确显示在文件系统中。 My output from debug statement shows file names and says: 我的debug语句输出显示文件名并显示:

/system/issues/covers//original/image_name.jpg?1377891456
[paperclip] Saving attachments.

but also shows nulls for DB values: 但还会显示数据库值的空值:

  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')

Thoughts? 有什么想法吗? Suggestions? 有什么建议吗? TIA. TIA。

So the final cause of this was the name of the columns in database weren't the same as the name using in the model. 因此,这的最终原因是数据库中列的名称与模型中使用的名称不同。 Somehow I had cover_image in migrations and cover in model. 不知何故,我在迁移中有cover_image,而在模型中有Cover。 Moral of the story, if you get stuck on this, make sure you check column names first . 故事的寓意,如果您对此感到困惑,请确保首先检查列名

Check your params hash, but I think what you'd be getting back from the form is just cover . 检查您的params哈希值,但是我认为您将从表格中获得的只是cover So use: 因此使用:

attr_accessible :cover

instead of all the column names. 而不是所有列名。 Paperclip sets those attributes internally, not through mass assignment. 回形针在内部设置这些属性,而不是通过批量分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM