我刚刚安装了回形针插件,收到以下错误消息,但我不确定原因:
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_paginateRuby .从我所能找到的,似乎是我的PostsController#index
有什么问题,或者可能是以前试图安装gem而不是插件,在这种情况下,我读到我应该能够通过/config/environments.rb
文件以某种方式补救.
我不认为以前的gem安装会有什么影响,因为我在安装插件之前就把旧版本的网站弄坏了.在网站的当前版本中,我展示了迁移后,该表已使用回形针列进行了更新.这是我的代码:
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 %>