53

I just installed the plugin for Paperclip and I am getting the following error message but I am not sure why:

NoMethodError (undefined method `has_attached_file' for #<Class:0x10338acd0>):
  /Users/bgadoci/.gem/ruby/1.8/gems/will_paginate-2.3.12/lib/will_paginate/finder.rb:170:in `method_missing'
  app/models/post.rb:2
  app/controllers/posts_controller.rb:50:in `show'

It is referencing the will_paginate gem. From what I can find, it seems that either there is something wrong with my PostsController#index or perhaps a previously attempt at installing the gem instead of the plugin, in which case I have read I should be able to remedy through the /config/environments.rb file somehow.

I didn't think that previous gem installation would matter as I did it in an old version of the site that I trashed before installing the plugin. In the current version of the site I show that the table has been updated with the Paperclip columns after migration. Here is my code:

PostsConroller#show:

  def show
    @post = Post.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @post }
    end
  end

Post model:

class Post < ActiveRecord::Base

  has_attached_file :photo
  validates_presence_of :body, :title
  has_many :comments, :dependent => :destroy
  has_many :tags, :dependent => :destroy
  has_many :votes, :dependent => :destroy
  belongs_to :user
  after_create :self_vote
      def self_vote
       # I am assuming you have a user_id field in `posts` and `votes` table.
       self.votes.create(:user => self.user)
      end

  cattr_reader :per_page 
    @@per_page = 10

end

/views/posts/new.html.erb:

<h1>New post</h1>
<%= link_to 'Back', posts_path %>
<% form_for(@post, :html => { :multipart => true}) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>
  <p>
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </p>
  <p>
    <%= f.file_field :photo %>
  </p>

  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>
the Tin Man
  • 150,910
  • 39
  • 198
  • 279
bgadoci
  • 6,083
  • 16
  • 60
  • 89
  • The error is coming from the PostsController "show" method, but you've got the "index" method and the contents of the "new" view pasted in there? Clean up your question to include the relevant information, and it's more likely that someone can help you figure this out. – jdl Apr 18 '10 at 04:03
  • 1
    Also, I can almost guarantee that the biggest clue there is the fact that the error is referencing Class and not Post. It should be fairly obvious once we see the actual code that is failing. – jdl Apr 18 '10 at 04:04
  • just updated for the show method. Sorry about that. New to all of this. Thanks for the direction. – bgadoci Apr 18 '10 at 04:06

5 Answers5

183

It is very important that you restart your server after installing new gems/plugins. This should solve your problem

Raunak
  • 6,177
  • 9
  • 36
  • 52
  • fixed my problem as well and it was also with mongrel... not used to using it so i guess you need to restart that bad boy a lot when you change plugins and/or configs. – pjammer Feb 14 '12 at 12:13
  • Had to restart my console too, reload! does not work. – Anjan Nov 02 '15 at 07:46
8

I'd suggest installing paperclip gem. Then you'd just need to add config.gem 'paperclip' to your environment.rb and run sudo rake gems:install.

Eimantas
  • 47,394
  • 15
  • 127
  • 164
  • Hello..I am using Rails 2.0.2 and ruby 1.8.7 n I am still facing this error.. I have added a require "paperclip" in my environment.rb file and I have installed paperclip 2.3.8 as my activerecord and activesupport is of the version 2.0.2. Any suggestions on what I can do to fix this..? Is there any way I can get the plugin for paperclip? ...as an alternative as I'm using an older rails version n plugins still work. I heard that thoughtbot has closed down their svn rep, can i somehow get it via github..?? – boddhisattva May 14 '11 at 12:15
  • @boddhisattva just update your gemfile like so: gem 'paperclip', :git => "git://github.com/thoughtbot/paperclip.git" – Gerard Jan 24 '12 at 12:29
  • @Gearóid : Thanks, but I was on a Rails 2.0.x project when I posted this question. The work around that did the trick for me was I had to find a paperclip commit compatible with the Rails version I was on. – boddhisattva Jan 25 '12 at 12:57
  • You really shouldn't be using 'sudo' to install gems. – RonLugge Nov 25 '14 at 23:49
2

create the file paperclip.rb inside the config/initializers/paperclip.rb

Add the below lines and restart the server

require "paperclip/railtie"

Paperclip::Railtie.insert

kotesh
  • 66
  • 4
1

I got this error spontaneously on 2 different dev machines after Paperclip was working fine for weeks.

spring stop

then restarted my rails console was needed

chris finne
  • 2,251
  • 22
  • 16
0

I guess this should have been obvious, but I'm using mongo/mongoid as my data layer and needed to install mongoid paperclip for it to work.

TheRightChoyce
  • 2,954
  • 1
  • 19
  • 17