unmuse
unmuse

Reputation: 687

Rails + Paperclip not giving image url in view

I am setting up a basic blog with an optional image upload for the posts. The images are being uploaded properly and going to the right directory. However, when I go to the view it loads the default image:

photos/original/missing.png

Here is the model

class Post < ActiveRecord::Base
  attr_accessible :body, :date, :feature, :poster, :title, :photo

  has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" },
                    :url  => "/assets/posts/:id/:style/:basename.:extension",
                    :path => ":rails_root/public/assets/posts/:id/:style/:basename.:extension"

  attr_accessor :photo_file_name
  attr_accessor :photo_content_type
  attr_accessor :photo_file_size
  attr_accessor :photo_updated_at
end

And in the view:

<%= image_tag @post.photo.url %>

For example, I upload an image with a post, and it gets uploaded to:

rails_root/public/assets/posts/5/original/image.jpg
rails_root/public/assets/posts/5/medium/image.jpg
rails_root/public/assets/posts/5/thumb/image.jpg

Migration

class AddAttachmentImageToPosts < ActiveRecord::Migration
  def self.up
    add_attachment :posts, :photo
  end

  def self.down
    remove_attachment :posts, :photo
  end
end

Schema:

  create_table "posts", :force => true do |t|
    t.string   "title"
    t.text     "body"
    t.datetime "date"
    t.string   "poster"
    t.boolean  "feature"
    t.datetime "created_at",         :null => false
    t.datetime "updated_at",         :null => false
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
  end

Yet when the view is rendered, it can't find the image. What am I missing here?

Upvotes: 0

Views: 1123

Answers (1)

rovermicrover
rovermicrover

Reputation: 1453

Try attr_accessible instead of attr_accessor for the photo columns.

so

class Post < ActiveRecord::Base

  attr_accessible :body, :date, :feature, :poster, :title, :photo, :photo_file_name, :photo_content_type, :photo_file_size, :photo_updated_at

  has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" },
                :url  => "/assets/posts/:id/:style/:basename.:extension",
                :path => ":rails_root/public/assets/posts/:id/:style/:basename.:extension"
end

EDIT AFTER UPDATE:

There is a mismatch between your DB and your paperclip settings. Either change all the columns to photo_x or change your settings that say photo to image.

Upvotes: 1

Related Questions