Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert NoMethodError for has_attachme...

Replytotopic

NoMethodError for has_attachment

Posted in Forums : Ask a Rails expert

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

I just started learning RoR this month so I’m sure I’ve just missed something here…

I’m using the attachment_fu plugin.
My current test app is very similar to this:
Tea Poci’s Create Photo Gallery less than 10 MINUTES

script/console says:

NoMethodError: undefined method `has_attachment' for #<Class:0xb764e5e4>

However, I’ve installed attachment_fu properly. I also compiled ImageMagick today and I know that’s installed properly.

I installed attachment_fu by:

$ cd /path/to/my/app/root
$ ruby ./script/plugin install  <a href="http://svn.techno-weenie.net/projects/plugins/attachment_fu/">http://svn.techno-weenie.net/projects/plugins/attachment_fu/</a>

I also have the rmagick gem installed.

Any idea what I could be doing wrong?

 
Jacob_duursma

Authority 12
Posting Rating 0
Sign in to rate this post

This may sound silly, but did you restart your (mongrel) server after the plugin install? And you didn’t call your class ‘File’ or something like that did you?

 
Profile

Authority 12
Posting Rating 76
Sign in to rate this post

After install the plugin or gem you have to restart your server after that the change is reflect ..

 
Self_portrait

Authority 37
Posting Rating 0
Sign in to rate this post

If that is literally what you ran to install attachment_fu then your problem is the HTML around the repository location, it should be:
ruby ./script/plugin install http://svn.techno-weenie.net/projects/plugins/attachment_fu/

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

oh haha, i didn’t know i had to restart WEBrick. i don’t think i did.

Well, i just restarted WEBrick and I get another error.

First let me tell you, I followed the PDF titled “Upload Images with Thumbnails” from this page: http://pragprog.com/titles/fr_arr/advanced-rails-recipes

I followed it word for word and I got this error:

Mysql::Error: Table 'attachment_fu_example_development.albums' doesn't exist: SHOW FIELDS FROM `albums`

It looks as though the author neglected to mention that I have to create an Album table as well? It’s also possible that since I downloaded just one chapter, the previous chapter had already mentioned that.

Anyway, is that the problem here? And if so, would an Albums table with the following structure suffice:
title :string
artist : string
cover_id :int

Jim Remsik,
actually, I think the pre tag on this forum added the HTML anchor tag…maybe it first processes the BBCode and then applies the pre tag?

Thanks :)

 
Me

Authority 37
Posting Rating 100
Sign in to rate this post

Have you read the instructions by Rick himself? – http://svn.techno-weenie.net/projects/plugins/attachment_fu/README
Personally I think they are pretty clear on what you need to do …

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

@Clemens Kofler,

Thanks for that suggestion :)
I actually thought a tutorial might be easier to follow but the README was surprisingly simple.

I gave it a shot and got it all working although I think I may have done something wrong.

I created a table via migrations with the fields required for attachment_fu..i also added a few other fields to this same table:

mysql> describe wraps;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment | 
| name         | varchar(255) | YES  |     | NULL    |                | 
| description  | varchar(255) | YES  |     | NULL    |                | 
| price        | decimal(8,2) | YES  |     | 0.00    |                | 
| image        | varchar(255) | YES  |     | NULL    |                | 
| created_at   | datetime     | YES  |     | NULL    |                | 
| updated_at   | datetime     | YES  |     | NULL    |                | 
| size         | int(11)      | YES  |     | NULL    |                | 
| content_type | varchar(255) | YES  |     | NULL    |                | 
| filename     | varchar(255) | YES  |     | NULL    |                | 
| height       | int(11)      | YES  |     | NULL    |                | 
| width        | int(11)      | YES  |     | NULL    |                | 
| parent_id    | int(11)      | YES  |     | NULL    |                | 
| thumbnail    | varchar(255) | YES  |     | NULL    |                | 
+--------------+--------------+------+-----+---------+----------------+
14 rows in set (0.00 sec)

also, my model looks like this:

  1 class Wrap < ActiveRecord::Base
  2   has_attachment  :content_type => :image,
  3                   :max_size => 500.kilobyte,
  4                   :resize_to => [100,100],
  5                   :thumbnails => {:kurumitta => [20,20], :geometry => 'x50'},
  6                   :storage => :file_system
  7 
  8   validates_as_attachment
  9 end

so now, since i have ‘kurumitta’ and ‘geometry’ as my thumbnails, it looks like attachment_fu is creating 3 entries in that table (the third is for the original image) on every submit.

so i guess i should have two separate tables? but how would i link the attachment_fu table (lets call that Table A) to the other table which contains my items description, price and name? (call this Table B)

Thanks for your time so far.

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

I think I’ve figured it out.

If the attachment_fu table was called ‘photos’ and the other table was called ‘wraps’, then my models for those two tables should use relationships (has_one and belongs to), right?

and i should add an coumn named wraps_id to the photos table, right?

 
Me

Authority 37
Posting Rating 100
Sign in to rate this post

With attachment_fu you will usually have two separate models … In one of my apps, for example, I have an Event that has_many :photos. The attachment_fu columns sit in the photos table. If you want a file upload plugin that doesn’t require a separate model and table, you could use Paperclip – I’ve used it for two projects now and it works even better than attachment_fu.

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

@Clemens Kofler,
Thanks again.

I used Paperclip and got most of it working.
I used the README as well as
Jim Neath’s Paperclip: Attaching Files in Rails on April 17th, 2008

i do have a problem though, only the default image gets saved, the dimensions defined in :styles don’t get saved.

this is my model:

  1 class Wrap < ActiveRecord::Base
  2   has_attached_file :photo,
  3                     :styles => {:thumb => "100x100#",
  4                                 :small => "150x150>"},
  5                     :whiny_thumbnails => true
  6 end

since i have :whiny_thumbnails set to true, this is the error i get once i submit:

    * Photo /tmp/stream.14642.0 is not recognized by the 'identify' command.
    * Photo /tmp/stream.14642.0 is not recognized by the 'identify' command.

Also, script/console shows me this:

ActionController::RoutingError (No route matches "/photos/5/thumb/Ubuntu.png" with {:method=>:get}):
.
.
.

ActionController::RoutingError (No route matches "/photos/5/small/Ubuntu.png" with {:method=>:get}):
.
.
.

there’s a lot more text in the two lines of error messages above, but i truncated it to just what i think will be helfpul to figure out the issue.

do i have to use Paperclips :path option so solve this? i think that might be it because this is my current directory structure for Paperclips folder:

$ ls -hR public/photos/5/
public/photos/5/:
original

public/photos/5/original:
Ubuntu.png

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

Also, how would I modify the edit view to accept a new image, supposing the user wanted to replace the current image with another one?

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

taking another look at the log output, it looks like it’s a routing error, right?

so it looks like i will have to either change ’:path’ in my model for the has_attached_file option or add a line to my routing.rb. it looks like the prior is the more elegant solution.
only, i don’t know how i should alter the ’:path’

 
Me

Authority 37
Posting Rating 100
Sign in to rate this post

You need to configure the paths accordingly. Here’s what I have in a project:
has_attached_file :image, :path => ":rails_root/public/images/products/:id.:extension", :url => "/images/products/:id.:extension"
This puts the uploaded files in /public/images/products/1.jpg if the product’s id is “1” and the file type is “jpg”. This way, I can use it with the image_tag helper. You should also note that I don’t use thumbnails, therefore there’s only one type of images and I don’t use the :style option.

There’s lots of options, so it’s best to take a look at the file vendor/plugins/paperclip/lib/paperclip/attachment.rb – it shows you most of the options including so-called interpolations (like :rails_root, :class, etc.).

HTH

Replytotopic

Other Recent Topics

Ask a Rails expert : First post, requesting sage perspective

Ask a Rails expert : How to use mephisto

Ask a Rails expert : How to use mephisto

Ask a Rails expert : will_paginate customization problem

Ask a Rails expert : BackgroundRB still wants 'development' environment...?

Ask a Rails expert : activescaffold, sql exception

Ask a Rails expert : Passing non-english chars in query string

Ask a Rails expert : Rails and 2D barcodes

Ask a Rails expert : apache giving proxy error

Ask a Rails expert : Custom Responses w/ 'extra' information...?

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel