为什么在安装PaperClip时会得到"has_attached_file"的"未定义方法"?

bga*_*oci 52 ruby ruby-on-rails paperclip

我刚刚安装了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'
Run Code Online (Sandbox Code Playgroud)

它引用了will_paginate gem.从我能找到的,似乎我PostsController#index或者以前尝试安装gem而不是插件有问题,在这种情况下我读过我应该能够以/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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

/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 %>
Run Code Online (Sandbox Code Playgroud)

Rau*_*nak 178

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


Eim*_*tas 8

我建议安装paperclip gem.然后你只需要添加config.gem 'paperclip'到environment.rb并运行sudo rake gems:install.