Paperclip Trouble - Not Writing to the Database

Hello all,

I'm a Rails newbie trying to get a basic implementation of Paperclip up
but it's giving me problems. I'm developing on Windows XP (I know...)
with WEBrick and MySQL.

In my model I have:

class User < ActiveRecord::Base
  has_attached_file :avatar,:styles => { :medium => "300x300>", :thumb
=> "100x100>" }, :url => "/public/data/", :path =>
":rails_root/public/data/"
end

I added the path and URL options because it wasn't working under the
default at all, same problem. In my view:

<% form_for @user, :html => { :multipart => true } do |f| %>
  <%= f.error_messages %>
  <div class="field">
   <%= f.label :name %><br />
   <%= f.text_field :name %>
  </div>
  <div class="field">
   <%= f.label :email %><br />
   <%= f.text_field :email %>
  </div>
  <div class="field">
   <%= f.label :password %><br />
   <%= f.password_field :password %>
  </div>
  <div class="field">
   <%= f.label :password_confirmation, "Confirmation" %><br />
   <%= f.password_field :password_confirmation %>
  </div>
  <div class="field">
   <%= f.file_field :avatar %>
  </div>
  <div class="actions">
    <%= f.submit "Sign Up" %>
  </div>

In my controller:

  def create
    @user = User.new(params[:user])
     if @user.save
      sign_in @user
      flash[:success] = "Welcome"
      redirect_to @user
    else
      @user.password = ""
      @user.password_confirmation = ""
      @title = "Sign Up"
      render 'new'
    end
  end

When I try to create a new user with an avatar the SQL (from the server
logs) is:

INSERT INTO `users`
(`name`,
  `email`,
  `created_at`,
  `updated_at`,
  `encrypted_password`,
  `salt`,
  `remember_token`,
  `admin`,
  `avatar_file_name`,
  `avatar_content_type`,
  `avatar_file_size`,
  `avatar_updated_at`)
VALUES
  ('Example User',
   'foo@baz.net',
   '2010-08-07 00:18:52',
   '2010-08-07 00:18:52',
   'e49ab5f50085e13f986e0a89acd3d709131b45c3bb8a74fae69a8b8e58c0fcd3',
   '90bf715adac888a8d6c88061dbdb9df368eabe7ac2317a432c34c0d48e12defa',
   NULL,
   0,
   NULL,
   NULL,
   NULL,
   NULL)

When I look at the new user the avatar is replaced by the text "Missing"
and the HTML generated is:
<img alt="Missing" src="/avatars/original/missing.png" />

If I check where the src link goes it gives me:
<h1>Routing Error</h1>
<p><pre>No route matches &quot;/avatars/original/missing.png&quot; with
{:method=&gt;:get}</pre></p>

I really don't know what the problem is. I've been following the Quick
Start guide (from http://github.com/thoughtbot/paperclip) and
cross-referencing from other guides like Railscast, but none of them
seem to help. Sorry this is so long; I'm sure the solution is something
simple I overlooked but I have no idea where.

This is wrong: :url => "/public/data/", :path =>
":rails_root/public/data/"

It should look like:

:url => "/public/data/:style/:basename.:extension",
:path => ":rails_root/public/data/:style/:basename.:extension"

Thank you, I changed it. But it's still not fixing the SQL query.

By the way, is there a good way to step through a Rails application? I
don't know if it would help here, but I like being able to see what's
going on at every level. I could at least see what's being used to make
the query.

Have a look at the Rails Guide on debugging. It will show you how to
use ruby-debug to break in and step through. Also I suggest that you
go through all the other guides there.

Colin

A. Leek wrote:

But it's still not fixing the SQL query.

Then I'd guess you have attr_accessible? I have fallen into that trap in
the past too :wink:

I noticed in your form that you didnt specify any of the support
fields that paperclip uses -avatar_file_name for example - if these
aren't in the params hash they won't be saved in the db. In a recent
project I added all the fields to the form, but you could add them as
hidden.

Paperclip doesn't store the image in the database so is an actual
image being stored in the location you setup?

go to railscasts.com, LESS writing more WATCHING in real tuts

Bb Serviss wrote:

I noticed in your form that you didnt specify any of the support
fields that paperclip uses -avatar_file_name for example - if these
aren't in the params hash they won't be saved in the db. In a recent
project I added all the fields to the form, but you could add them as
hidden.

Paperclip doesn't store the image in the database so is an actual
image being stored in the location you setup?

I'm not sure what you mean by support fields...I looked around, but
couldn't find much clarification.

I know that Paperclip doesn't store the actual image in the database,
that would be silly :slight_smile: But it's not saving anything to the database in
the avatar_file_name, _content_type, _file_size, or _updated_at fields,
they're just NULL.

Anyway, I followed Fernando Perez's advice about attr_accessible and it
sort of worked, now I'm getting a different error: Avatar
C:/DOCUME~1/mars/LOCALS~1/Temp/stream,5388,3.jpg is not recognized by
the 'identify' command.

I poked around and it seemed to be a problem with ImageMagick, which I
didn't have installed. I installed it and checked that the PATH was
right, but it keeps giving the the same error. Looking around, I tried
the solution here:
http://snipplr.com/view/29445/getting-paperclip-working-in-windows/
changing the ImageMagick location to what I had in the PATH, but it
keeps giving me the same error.

I didn't see the avatar_file_size, avatar_content_type, and
avatar_created_at fields in your form. I don't know if these are
required though as they should be created when you submit a
file_field.

You can store the images in the database if you want to, but most
would recommend you don't.

Have you looked in the location to see if an image was being saved?

Have you asked on the paperclip Google group?

A. Leek wrote:

I poked around and it seemed to be a problem with ImageMagick, which I
didn't have installed. I installed it and checked that the PATH was
right, but it keeps giving the the same error. Looking around, I tried
the solution here:
http://snipplr.com/view/29445/getting-paperclip-working-in-windows/
changing the ImageMagick location to what I had in the PATH, but it
keeps giving me the same error.

There is a duality to Paperclip in that it is meant to process anything,
but the default processor (Thumbnail) is for images. I'm guessing the
versatility of it is why it doesn't do a hard check for ImageMagick
before it tries to process anything.

As far as the Paperclip Processor knows the command just fails, and
therefor processing fails, and therefore there is no image to link to. I
highly recommend writing a test that just checks that the path you're
using for ImageMagick exists. Something as simple as:

IMAGE_MAGICK_PATH = "/my/path/to/imagemagick"

def test_image_magick_is_installed
  assert File.exist?(IMAGE_MAGICK_PATH)
end

At least then you know that your install is working.

Parker Selbert wrote:

There is a duality to Paperclip in that it is meant to process anything,
but the default processor (Thumbnail) is for images. I'm guessing the
versatility of it is why it doesn't do a hard check for ImageMagick
before it tries to process anything.

As far as the Paperclip Processor knows the command just fails, and
therefor processing fails, and therefore there is no image to link to. I
highly recommend writing a test that just checks that the path you're
using for ImageMagick exists. Something as simple as:

IMAGE_MAGICK_PATH = "/my/path/to/imagemagick"

def test_image_magick_is_installed
  assert File.exist?(IMAGE_MAGICK_PATH)
end

At least then you know that your install is working.

Well I tried that, and it comes back false... The error messages give
the right path for ImageMagick, but they still fail on the identify
command. I've tested "identify" from the command line, and it works
fine. I'm pretty lost as to what to do now, Paperclip seems to
understand there's ImageMagick in Program Files, they just don't want to
talk to each other.

The images do get to a temp location in Local Settings\Temp, so some
moving around is happening. Honestly, I'm just trying to get files
uploaded in general, I'm only testing it on images because that's what
all the guides are for and I wanted to have something working before I
generalized. This is getting a lot more problematic than I expected. I
guess the next thing to do is go to the Paperclip Google Group and see
if they know anything.

A. Leek wrote:

Well I tried that, and it comes back false... The error messages give
the right path for ImageMagick, but they still fail on the identify
command. I've tested "identify" from the command line, and it works
fine. I'm pretty lost as to what to do now, Paperclip seems to
understand there's ImageMagick in Program Files, they just don't want to
talk to each other.

Ah, I think I see a potential problem. It's actually a pretty common
thing in Windows development using Perl, Ruby etc. When you're using the
shell Windows will handle the PATH including "Program Files" as
"Program\ Files" without you having to do anything. When Paperclip tries
to run the command through Ruby your request gets broken up at the space
between "Program Files", and it is shown as two different commands:

C:\Program
Files\ImageMagick\imagemagick.exe

Or something to that effect. If it isn't happening with Program Files it
could just as easily by happening with your temporary content, which
also has a space in the path:

identify C:\Local
Settings\Temp\crazyfilename,0,1.jpg

The images do get to a temp location in Local Settings\Temp, so some
moving around is happening. Honestly, I'm just trying to get files
uploaded in general, I'm only testing it on images because that's what
all the guides are for and I wanted to have something working before I
generalized. This is getting a lot more problematic than I expected. I
guess the next thing to do is go to the Paperclip Google Group and see
if they know anything.

Stick with it, in the end it really does make things easier. Keep in
mind that all of these libraries aren't developed with WIndows in mind
and you'll really have to compensate. If possible pretend your system is
*nix, keep your installs in C:\lib or C:\bin and remove the path issues.

Parker Selbert wrote:
[...]

Keep in
mind that all of these libraries aren't developed with WIndows in mind
and you'll really have to compensate. If possible pretend your system is
*nix, keep your installs in C:\lib or C:\bin and remove the path issues.

Or don't pretend. Rails development on Windows is problematic, from all
the info I have (note: I don't use Windows myself). So if you can't get
your hands on a Mac or other *nix system, at least install a *nix VM
(Virtual Rails has been making a splash lately).

Best,