Skip to content

Paperclip and ImageMagick and a Weird Problem — in RoR

by dkberktas on July 13th, 2010

As promised in the last post, here is the Paperclip and ImageMagick as an extension to railstutorial.

For image upload and manipulation, Paperclip seems to be the solutions. The following links does a great job of describing the installation process and basic usage, so I have nothing to add here.

So far so good, but if you are following railstutorial, you will probably doing this stuff to micropost, and you will probably get an error like this,

  1. “missing” image replacement in microspots
  2. In the log/development.log file,
ActionController::RoutingError (No route matches “/photos/original/missing.png” with {:method=>:get}):
Rendering rescues/layout (not_found)

Solution:

If you carefully inspect the log file you can catch the line

WARNING: Can’t mass-assign these protected attributes: photo
[4;36;1mMicropost Create (0.4ms)[0m [0;1mINSERT INTO “microposts” (“created_at”, “photo_file_size”, “updated_at”, “photo_file_name”, “photo_content_type”, “user_id”, “photo_updated_at”, “content”)

Just with just one line, open your model and fix the accessibility:

class Micropost &  ActiveRecord::Base
  attr_accessible :content, :photo
...

ps. In case you are a noob like me in RoR, this link shows howto uninstall the paperclip plug in.

ps2. Another possible problem might be your ImageMagick installation, make sure you have the correct path by trying convert and identify command. In case add the path to development.rb as described this Stackoverflow entry.

Summary of Steps to add Paperclip

  • script/plugin install git://github.com/thoughtbot/paperclip.git
  • script/generate paperclip product photo
  • In the model
has_attached_file :photo
  • In the form

header part

<% form_for @micropost do |f| %>
<% form_for @micropost, :html => { :multipart => true} do |f| %>

body part

<p>
<%= f.file_field :photo %>
</p>
  • To show the uploaded file
<%= image_tag @product.photo.url %>

From → english, tech

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS