Ruby-on-rails 为什么我在安装 PaperClip 时得到“has_attached_file”的“未定义方法”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2661062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:32:43  来源:igfitidea点击:

Why do I get an "undefined method for `has_attached_file` when installing PaperClip?

ruby-on-railsrubypaperclip

提问by bgadoci

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#indexor 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.rbfile somehow.

它引用 will_paginate gem。据我所知,似乎是我的PostsController#index或之前尝试安装 gem 而不是插件有问题,在这种情况下,我已经阅读过我应该能够以/config/environments.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#show

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

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @post }
    end
  end

Postmodel:

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:

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

回答by Raunak

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

安装新的 gems/插件后重新启动服务器非常重要。这应该可以解决您的问题

回答by Eimantas

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。然后你只需要添加config.gem 'paperclip'到你的 environment.rb 并运行sudo rake gems:install.

回答by kotesh

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

在 config/initializers/paperclip.rb 中创建文件 paperclip.rb

Add the below lines and restart the server

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

require "paperclip/railtie"

需要“回形针/轨道”

Paperclip::Railtie.insert

回形针::Railtie.insert

回答by chris finne

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

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

spring stop

spring stop

then restarted my rails console was needed

然后重新启动需要我的 rails 控制台

回答by TheRightChoyce

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

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