Hello,
I am trying to use the paperclip plugin in my rails app to upload images
for posts. I am using Rails 2.0.2 and ruby 1.8.7 on Ubuntu 10.04 os for
project specific purposes.
I am referring to the following tutorials for this :-
1. http://railscasts.com/episodes/134-paperclip?view=comments
2.
http://jimneath.org/2008/04/17/paperclip-attaching-files-in-rails.html
I did a git clone on the paperclip plugin via:-
https://github.com/thoughtbot/paperclip , but the command like `ruby
script/generate paperclip group_post photo` didn't generate a migration
for it. I then created a migration for the same through ruby
script/generate and uploaded the appropriate columns of my table
`group_posts`.
I then tried to install the gem and since I am using activerecord and
activesupport 2.0.2 I used paperclip 2.3.8 gem from rubygems.org.The
undefined method error still persisted, I really couldn't figure out
why.
Now here.. the command `ruby script/generate paperclip group_post
photo` worked perfectly fine for me.
The current error I am getting exactly looks like this:-
undefined method `has_attached_file' for #<Class:0xb67119f0>
The **Application Trace** for the same is:-
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activerecord-2.0.2/lib/active_record/base.rb:1532:in
`method_missing_without_paginate'
vendor/plugins/will_paginate/lib/will_paginate/finder.rb:170:in
`method_missing'
app/models/group_post.rb:9
app/controllers/groups_controller.rb:27:in `show'
The **Full Trace** for this just in case is:-
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activerecord-2.0.2/lib/active_record/base.rb:1532:in
`method_missing_without_paginate'
vendor/plugins/will_paginate/lib/will_paginate/finder.rb:170:in
`method_missing'
app/models/group_post.rb:9
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:203:in
`load_without_new_constant_marking'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:203:in
`load_file'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:342:in
`new_constants_in'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:202:in
`load_file'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:94:in
`require_or_load'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:248:in
`load_missing_constant'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:453:in
`const_missing_not_from_s3_library'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/aws-s3-0.6.2/lib/aws/s3/extensions.rb:206:in
`const_missing'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:465:in
`const_missing'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:471:in
`send'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:471:in
`const_missing'
app/controllers/groups_controller.rb:27:in `show'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158:in
`send'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158:in
`perform_action_without_filters'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/filters.rb:697:in
`call_filters'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/filters.rb:689:in
`perform_action_without_benchmark'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in
`perform_action_without_rescue'
/home/mohnish/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/benchmark.rb:293:in
`measure'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in
`perform_action_without_rescue'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/rescue.rb:199:in
`perform_action_without_caching'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/caching.rb:678:in
`perform_action'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in
`cache'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activerecord-2.0.2/lib/active_record/query_cache.rb:8:in
`cache'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/caching.rb:677:in
`perform_action'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/base.rb:524:in
`send'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/base.rb:524:in
`process_without_filters'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/filters.rb:685:in
`process_without_session_management_support'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/session_management.rb:123:in
`process_without_test'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/test_process.rb:15:in
`process'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/base.rb:388:in
`process'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:171:in
`handle_request'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:115:in
`dispatch'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:126:in
`dispatch_cgi'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:9:in
`dispatch'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel/rails.rb:76:in
`process'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel/rails.rb:74:in
`synchronize'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel/rails.rb:74:in
`process'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:159:in
`process_client'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:158:in
`each'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:158:in
`process_client'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:285:in
`run'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:285:in
`initialize'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:285:in
`new'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:285:in
`run'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:268:in
`initialize'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:268:in
`new'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel.rb:268:in
`run'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:282:in
`run'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:in
`each'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:in
`run'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/bin/mongrel_rails:128:in
`run'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/lib/mongrel/command.rb:212:in
`run'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/mongrel-1.1.5/bin/mongrel_rails:281
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:489:in
`load'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:489:in
`load'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:342:in
`new_constants_in'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:489:in
`load'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/rails-2.0.2/lib/commands/servers/mongrel.rb:64
/home/mohnish/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require'
/home/mohnish/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:496:in
`require'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:342:in
`new_constants_in'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:496:in
`require'
/home/mohnish/.rvm/gems/ruby-1.8.7-p334/gems/rails-2.0.2/lib/commands/server.rb:39
/home/mohnish/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require'
/home/mohnish/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
script/server:3
My groups_controller.rb show method looks like this, here line 27 is
`@group_post = GroupPost.new(params[:group_post])` :-
def show
@investor_group = InvestorGroup.find(params[:id])
@members = @investor_group.activated_members
# code taken from discuss method
investor_id = session['investor_id']
@investor = Investor.find(investor_id)
@members = @investor_group.activated_members
unless
@current_user.is_an_existing_member_of_group(@investor_group)
flash[:notice] = "Please join this group to participate in
discussions"
redirect_to :action => :show, :id => @investor_group and
return
else
@group_post = GroupPost.new(params[:group_post]) # line 27,
tried GroupPost.create also.. seems to be of no avail..
end
#@group_post = GroupPost.new(params[:group_post])
@group_post.investor_group_id = @investor_group.id
@group_post.post_by = investor_id
unless @group_post.message.blank?
if @group_post.save
flash[:notice] = 'Post was successfull'
else
flash[:notice] = 'Post was not successfull'
end
#redirect_to :action => :show, :id => @investor_group and
return to change.. appropriately....
end
#if @group_post.message.blank?
# flash[:notice] = 'Post can\'t be blank.'
#end
@group_all_posts = GroupPost.find(:all, :conditions => [
'investor_group_id = ?', "#{@investor_group.id}" ], :order =>
'created_at DESC')
# code taken from discuss method
end
The group_post model looks like this with line 9 having the following
code has_attached_file :photo :-
class GroupPost < ActiveRecord::Base
has_many :group_comments
belongs_to :investor_group
validates_presence_of :message
#include Paperclip
require "paperclip"
# Paperclip
has_attached_file :photo # line 9
end
Part of my view file where I am trying to upload the picture along with
a post looks like this:-
<%form_for :group_post, @group_post, :url => {:action =>
:show},:html => {:multipart => 'true'},:id => 'new_post' do |f| -%>
<p><label>Start Discussion:</label><br><%=f.text_field
:message%></p>
<%=f.file_field :photo%></p>
<p><%=submit_tag "Post"%></p>
<%end%>
I tried what was given in:-