为什么我要在安装PaperClip时获得“has_attached_file”的未定义方法?

时间:2022-08-22 22:43:41

I just installed the plugin for Paperclip and I am getting the following error message but I am not sure why:

我刚刚为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'

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.

它正在引用will_paginate。从我发现的情况来看,要么是我的PostsController#索引出了问题,要么是我之前试图安装gem而不是插件,在这种情况下,我应该能够通过/config/environment进行修复。rb文件。

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:

我不认为之前的gem安装会有什么影响,因为在安装插件之前,我使用了一个旧版本的站点。在站点的当前版本中,我展示了在迁移之后,表已经用Paperclip列进行了更新。这是我的代码:

PostsConroller#show:

PostsConroller #显示:

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

5 个解决方案

#1


172  

It is very important that you restart your server after installing new gems/plugins. This should solve your problem

在安装了新的gems/插件之后重新启动服务器是非常重要的。这应该能解决你的问题

#2


8  

I'd suggest installing paperclip gem. Then you'd just need to add config.gem 'paperclip' to your environment.rb and run sudo rake gems:install.

我建议你安装一个曲别针宝石。然后你只需要添加配置。在您的环境中使用gem 'paperclip'。rb和运行sudo rake gems:安装。

#3


1  

create the file paperclip.rb inside the config/initializers/paperclip.rb

创建文件回形针。在config /初始化/ paperclip.rb rb

Add the below lines and restart the server

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

require "paperclip/railtie"

需要“回形针/ railtie”

Paperclip::Railtie.insert

区别:Railtie.insert

#4


1  

I got this error spontaneously on 2 different dev machines after Paperclip was working fine for weeks.

在Paperclip正常工作数周后,我在两台不同的开发机器上自动地发现了这个错误。

spring stop

春天停止

then restarted my rails console was needed

然后重新启动rails控制台

#5


0  

I guess this should have been obvious, but I'm using mongo/mongoid as my data layer and needed to install mongoid paperclip for it to work.

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

#1


172  

It is very important that you restart your server after installing new gems/plugins. This should solve your problem

在安装了新的gems/插件之后重新启动服务器是非常重要的。这应该能解决你的问题

#2


8  

I'd suggest installing paperclip gem. Then you'd just need to add config.gem 'paperclip' to your environment.rb and run sudo rake gems:install.

我建议你安装一个曲别针宝石。然后你只需要添加配置。在您的环境中使用gem 'paperclip'。rb和运行sudo rake gems:安装。

#3


1  

create the file paperclip.rb inside the config/initializers/paperclip.rb

创建文件回形针。在config /初始化/ paperclip.rb rb

Add the below lines and restart the server

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

require "paperclip/railtie"

需要“回形针/ railtie”

Paperclip::Railtie.insert

区别:Railtie.insert

#4


1  

I got this error spontaneously on 2 different dev machines after Paperclip was working fine for weeks.

在Paperclip正常工作数周后,我在两台不同的开发机器上自动地发现了这个错误。

spring stop

春天停止

then restarted my rails console was needed

然后重新启动rails控制台

#5


0  

I guess this should have been obvious, but I'm using mongo/mongoid as my data layer and needed to install mongoid paperclip for it to work.

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