我刚安装了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。从我所能找到的,似乎我的PostsController#index
或者之前尝试安装gem而不是插件有问题,在这种情况下我已经读过我应该能够通过{ {1}}文件以某种方式。
我认为以前的gem安装并不重要,因为我在安装插件之前在旧版本的网站中进行了安装。在该站点的当前版本中,我显示该表已在迁移后使用Paperclip列进行更新。这是我的代码:
/config/environments.rb
:
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
答案 0 :(得分:177)
安装新的gem /插件后重启服务器非常重要。这应该可以解决你的问题
答案 1 :(得分:8)
我建议安装paperclip gem。然后,您只需将config.gem 'paperclip'
添加到您的environment.rb并运行sudo rake gems:install
。
答案 2 :(得分:1)
在config / initializers / paperclip.rb
中创建文件paperclip.rb添加以下行并重新启动服务器
需要“paperclip / railtie”
回形针:: Railtie.insert
答案 3 :(得分:1)
在Paperclip正常工作几周之后,我在两台不同的开发机器上自发地出现了这个错误。
| Domain | Method | URI | Name | Action | Middleware |
+--------+--------------------------------+------------------------------------------------+------------------------------------------+---------------------------------------------+--------------+
| | POST | notifications/{section_id} | notifications.store | App\Http\Controllers\TestController@store | web |
然后重启了我的rails控制台
答案 4 :(得分:0)
我想这应该是显而易见的,但我使用mongo / mongoid作为我的数据层,需要安装mongoid paperclip才能使用它。