Paperclip错误:’avatar_file_name’需要attr_accessor所需的模型

然后,我想使用Paperclip为每个清单提供照片。 我将相应的代码添加到列表show.html.erb,listing.rb模型,listings_controller.rb和_form.html.erb部分。

当我尝试上传列表的图像时,我收到此错误:

Paperclip::Error in ListingsController#update Listing model missing required attr_accessor for 'avatar_file_name' 

listing_controller的第44行:

 def update respond_to do |format| if @listing.update(listing_params) format.html { redirect_to @listing, notice: 'Listing was successfully updated.' } format.json { head :no_content } else 

要尝试的一些事情:即向listing.rb模型添加一些代码,以使:avatar的可接受图像更加健壮。 以下是几个stackoverflowpost中提到的添加到listing.rb模型的内容:

 validates_attachment_content_type :avatar, :content_type => %w(image/jpeg image/jpg image/png) 

不幸的是,当我附加图像时,我仍然会收到相同的错误。 当我没有附加图像时,我的默认图像被正确加载并正确创建列表。

我的清单模型:

 class Listing  { :medium => "150x", :thumb => "100x100>" }, :default_url => "default.jpg" validates_attachment_content_type :avatar, :content_type => %w(image/jpeg image/jpg image/png) end 

我的_form.html.erb部分:

  { :multipart => true } do |f| %>  

prohibited this listing from being saved:






我的listings_controller.rb控制器:

  def update respond_to do |format| if @listing.update(listing_params) format.html { redirect_to @listing, notice: 'Listing was successfully updated.' } format.json { head :no_content } else format.html { render action: 'edit' } format.json { render json: @listing.errors, status: :unprocessable_entity } end end end ... def listing_params params.require(:listing).permit(:name, :company, :email, :phone, :avatar) end 

还有我的schema.rb文件

 ActiveRecord::Schema.define(version: 20140329174335) do create_table "listings", force: true do |t| t.string "name" t.string "company" t.string "email" t.string "phone" t.datetime "created_at" t.datetime "updated_at" end end 

编辑:运行$ rails后添加控制台输出生成回形针列表头像

(我需要10个声望点才能发布,所以你必须找到链接http://sofzh.miximages.com/ruby-on-rails/c8KGTa3.png )

我想你忘了为listings中的avatar创建相应的字段。

我建议你生成一个迁移,将avatar添加到listings中,如下所示:

 rails generate paperclip listing avatar 

然后运行rake db:migrate

UPDATE

根据您的评论和编辑,您有一个迁移文件,用于将avatar添加到您通过运行rails generate paperclip user avatar创建的listings ,但遗憾的是由于某种原因它没有通过即,没有avatar specific fields("avatar_file_name", "avatar_content_type", "avatar_file_size" and "avatar_updated_at")根据您的db/schema.rblistings表中的avatar specific fields("avatar_file_name", "avatar_content_type", "avatar_file_size" and "avatar_updated_at") 。 这是一种非常奇怪的行为。

我建议你按顺序执行下面提到的步骤:

销毁现有迁移(如果有):

 rails destroy paperclip listing avatar 

生成新迁移:

 rails generate paperclip listing avatar 

 rake db:migrate 

更新2

我希望你没有拒绝投票给我(但是有人这样做了),所以我想提醒一下,这是Paperclip的一个持续性问题,我确实在我的评论中提出了一个解决方案(3月31日),如下所示:

我希望你在gemfile中尝试作为gem’paperclip’,:git =>“git://github.com/thoughtbot/paperclip.git”然后捆绑安装。 完成后告诉我

显然你没有注意到你今天投票给我的人。 另外,你说No errors as far as I can tell, image here: i.imgur.com/c8KGTa3.png但是如果你看一下输出有一个错误说清楚:

migration_file_name’:protected methodmigration_file_name’为PaperclipGenerator调用:0x007fb3c6494c20(NoMethodError)

从错误消息中,file_name在您尝试保存的模型中不可用。 我有类似的问题,并意识到我忘了运行Paperclip迁移:

rails generate paperclip [Model Name] [Attachment] (例如, rails g paperclip Images image

如果这不起作用,因为它所具有的问题是列“file_name”,请尝试将其添加到模型中(例如rails g migration addFilenameToImages file_name:string

这对我有用,所以希望它对你们有些帮助!

确保在paperclip_database:migration中使用复数表示新表名,并在paperclip生成器中使用单数:

 rails g paperclip_database:migration cms_article_category cms_article_category_images rails g paperclip cms_article_category cms_article_category_image 

并检查数据库中生成的列名称在您的示例中,avatar表中的列应称为avatar_file_name