NoMethodError: undefined method `add_attachment' for #<AddImageCloumnToPost:0x58 ba1b8>

2.8k views Asked by At

In this application I am trying to do image upload. When I run the migration I am getting error.

Controller:

class PostsController < ApplicationController
  def index
  end

  def new
    @post = Post.new
  end

  def
    @post = Post.new(post_params)
    @post.save
  end

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

  def edit
  end

  def delete
  end

  def post_params
    params.require(:post).permit(:image)
  end
end

Model:

class Post < ActiveRecord::Base

  has_attached_file :image
  validates_attachment_content_type :image, :content_type => ["image/jpg",
     "image/jpeg", "image/png", "image/gif"]            
end

Migration:

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|

      t.timestamps null: false
    end
  end
end


class AddImageCloumnToPost < ActiveRecord::Migration
  def up
    add_attachment :posts, :image
  end

  def down
    remove_attachment :posts, :image
  end
end

new.html.erb:

 <h1>Posts#new</h1>
  <%= form_for(@post, :html=>{ :multipart => true }) do |f| %>
  <%=   f.label :image %>
  <%= f.file_field :image %>
  <%= f.submit "Upload" %>
  <% end %>

  show.html.erb:

  <h1>Posts#Show</h1>
  <%= image_tag @post.image.url %>

Routes:

Rails.application.routes.draw do

  resources :posts
  get 'post/:id' => 'post#show'
  end

Error in console:

D:\imageupload>rake db:migrate
DL is deprecated, please use Fiddle
== 20150608132353 CreatePosts: migrating ======================================
-- create_table(:posts)
-> 0.0491s
== 20150608132353 CreatePosts: migrated (0.0491s) =============================

== 20150608132858 AddImageCloumnToPost: migrating =============================
-- add_attachment(:posts, :image)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

undefined method `add_attachment' for #<AddImageCloumnToPost:0x58ba1b8>D:/imageupload/db/migrate/20150608132858_add_image_cloumn_to_post.rb:3:in `up'
C:in `migrate'
NoMethodError: undefined method `add_attachment' for #<AddImageCloumnToPost:0x58
ba1b8>
D:/imageupload/db/migrate/20150608132858_add_image_cloumn_to_post.rb:3:in `up'
C:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
4

There are 4 answers

0
twonegatives On

add_attachment is a helper method provided by paperclip gem to add columns which would contain file_name, content_type, file_size of the attachment.

As your error states, Rails has no idea on what this method means when you call it in your migration file:

undefined method `add_attachment' for #<AddImageCloumnToPost:0x58ba1b8>D:/imageupload/db/migrate/20150608132858_add_image_cloumn_to_post.rb:3

The most obvious reason for this behaviour is that you did not require paperclip gem properly. Refer to its installation section on github and don't forget to run bundle install before usage.

0
Shadwell On

add_attachment is provided by the paperclip gem. You would need to include that in your Gemfile and run bundle install.

0
coderhs On

I faced the same issue, it was because i was refering to older version of the paperclip gem. Make sure you are using the latest gem in your Gemfile.

gem "paperclip", "~> 4.2"
1
Prashant4224 On

You missing self, Hope it helps

class AddImageCloumnToPost < ActiveRecord::Migration
  def self.up
    change_table :posts do |t|
      t.attachment :image
    end
  end

  def self.down
    remove_attachment :posts, :image
  end
end