简体   繁体   中英

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.

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??

Original Problem

I have a new rails project (3.2.13) with Paperclip 3 (3.4.2 in Gemfile.lock). 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

  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. ImageMagick is working and up to date. 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:

/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.

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. 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 . So use:

attr_accessible :cover

instead of all the column names. Paperclip sets those attributes internally, not through mass assignment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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