为什么在安装PaperClip时会得到一个“has_attached_file`的未定义方法?

我只是安装了Paperclip的插件,我收到以下错误消息,但我不知道为什么:

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' 

它引用了will_paginate gem。 从我能find的,似乎是我的PostsController#index或者以前尝试安装gem而不是插件,在这种情况下,我已经阅读我应该能够通过/config/environments.rb不知怎的, /config/environments.rb文件。

我不认为以前的gem安装会影响到我在安装插件之前旧版本的网站中所做的。 在该网站的当前版本中,我显示该表在迁移之后已经用Paperclip列进行了更新。 这是我的代码:

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模式:

 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 %> 

安装新的gem / plugins之后重新启动服务器是非常重要的。 这应该可以解决你的问题

我会build议安装回形针gem。 然后,您只需要将config.gem 'paperclip'添加到您的environment.rb并运行sudo rake gems:install

在config / initializers / paperclip.rb里创build文件paperclip.rb

添加下面的行并重新启动服务器

要求“回形针/ railtie”

回形针:: Railtie.insert

在Paperclip工作良好数星期之后,我在两台不同的开发机器上自发地发生了这个错误。

spring stop

然后重新启动我的轨道控制台是必要的

我想这应该是显而易见的,但我使用mongo / mongoid作为我的数据层,需要安装mongoid回形针才能正常工作。