一千萬個為什麽

搜索

為什麽在安裝PaperClip時會得到“has_attached_file”的“未定義方法”?

我剛安裝了Paperclip的插件,我收到以下錯誤消息,但我不確定原因:

NoMethodError (undefined method `has_attached_file' for #):
  /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:

New post

 { :multipart => true}) do |f| %>
  

  





最佳答案

安裝新的gem /插件後重啟服務器非常重要。這應該可以解決您的問題

轉載註明原文: 為什麽在安裝PaperClip時會得到“has_attached_file”的“未定義方法”?