- 首页
- 其他开发
- 回形针错误:模型缺少 'avatar_file_name' 所需的 attr_accessor
回形针错误:模型缺少 'avatar_file_name' 所需的 attr_accessor
[英] Paperclip Error: model missing required attr_accessor for 'avatar_file_name'
本文介绍了回形针错误:模型缺少 'avatar_file_name' 所需的 attr_accessor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
然后我想使用 Paperclip 为每个列表添加照片.我将适当的代码添加到列表 show.html.erb、listing.rb 模型、listings_controller.rb 和 _form.html.erb 部分.
当我尝试为列表上传图片时,出现此错误:
Paperclip::ListingsController 中的错误#update列出模型缺少 'avatar_file_name' 所需的 attr_accessor
listings_controller 的第 44 行:
def 更新response_to do |格式|如果@listing.update(listing_params)format.html { redirect_to @listing,注意:'Listing 已成功更新.'}format.json { 头:no_content }别的
要尝试的一些事情:即向listing.rb 模型添加一些代码,以使:avatar 的可接受图像更加健壮.以下是几篇stackoverflow帖子提到的添加到listing.rb模型的内容:
validates_attachment_content_type :avatar, :content_type =>%w(图像/jpeg 图像/jpg 图像/png)
不幸的是,我在附加图像时仍然遇到相同的错误.当我不附加图像时,我的默认图像加载正常并且列表创建正确.
我的列表模型:
类列表{:中 =>"150x", :thumb =>100x100"}, :default_url =>默认.jpg"validates_attachment_content_type :avatar, :content_type =>%w(图像/jpeg 图像/jpg 图像/png)结尾
我的 _form.html.erb 部分:
<%= form_for @listing, :html =>{ :multipart =>真 } 做 |f|%><% 如果@listing.errors.any?%><div id="error_explanation"><h2><%=pluralize(@listing.errors.count, "error") %>禁止保存此列表:</h2><ul><% @listing.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>
<%结束%><div class="form-group"><%= f.label :name %><br><%= f.text_field :name, class: "form-control" %>
<div class="form-group"><%= f.label :company %><br><%= f.text_field :company, class: "form-control" %>
<div class="form-group"><%= f.label :email %><br><%= f.text_field :email, class: "form-control" %>
<div class="form-group"><%= f.label :phone %><br><%= f.text_field :phone, class: "form-control" %>
<div class="form-group"><%= f.label :avatar %><br><%= f.file_field :avatar, class: "form-control" %>
<div class="form-group"><%= f.submit class: "btn btn-primary" %>
<%结束%>
我的 listings_controller.rb 控制器:
def 更新response_to do |格式|如果@listing.update(listing_params)format.html { redirect_to @listing,注意:'Listing 已成功更新.'}format.json { 头:no_content }别的format.html { 渲染动作:'编辑' }format.json { 渲染 json: @listing.errors, 状态: :unprocessable_entity }结尾结尾结尾...定义列表参数params.require(:listing).permit(:name, :company, :email, :phone, :avatar)结尾
还有我的 schema.rb 文件
ActiveRecord::Schema.define(version: 20140329174335) docreate_table "listings", force: true do |t|t.string "名称"t.string "公司"t.string "电子邮件"t.string "电话"t.datetime "created_at"t.datetime "updated_at"结尾结尾
在运行 $rails 后添加控制台输出生成回形针列表头像
(我需要 10 个声望点才能发布,所以你必须接受链接 http://i.imgur.com/c8KGTa3.png)
解决方案
我想你忘记在 listings
表中为 avatar
创建相应的字段.
我建议您生成迁移以将 avatar
添加到 listings
表,如下所示:
rails 生成回形针列表头像
然后运行rake db:migrate
更新
根据您的评论和编辑,您有一个迁移文件,用于将 avatar
添加到您通过运行 rails generate paperclip user avatar 创建的 listings
表中
code> 但不幸的是由于某种原因它没有通过,即,listings
中没有特定于头像的字段(avatar_file_name"、avatar_content_type"、avatar_file_size"和avatar_updated_at")
code> 表根据您的 db/schema.rb
.这是一个非常奇怪的行为.
我建议您按顺序执行以下步骤:
销毁现有迁移(如果有):
rails 销毁回形针列表头像
生成新的迁移:
rails 生成回形针列表头像
运行
rake db:migrate
更新 2
我希望你没有否决我(但有人拒绝了),所以我想提醒它注意这是 Paperclip 的一个持续问题,我确实在我的评论(3 月 31 日)中提出了一个解决方案,如下所示:
<块引用>我想让你尝试作为 gem 'paperclip', :git =>Gemfile 中的git://github.com/thoughtbot/paperclip.git"然后捆绑安装.完成后告诉我
显然,您或今天投票给我的人没有注意到这一点.另外,你说据我所知没有错误,图片在这里:
i.imgur.com/c8KGTa3.png 但是如果您查看输出有一个错误说明清楚:
<块引用>migration_file_name':受保护的方法migration_file_name' 调用PaperclipGenerator:0x007fb3c6494c20 (NoMethodError)
I then want to use Paperclip to have photos for each Listing. I added the appropriate code to the listings show.html.erb, the listing.rb model, the listings_controller.rb and the _form.html.erb partial.
When I try uploading an image for the Listing I get this error:
Paperclip::Error in ListingsController#update
Listing model missing required attr_accessor for 'avatar_file_name'
Line 44 of listings_controller:
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
A few things to try: namely adding some code to the listing.rb model to make the acceptable images for the :avatar more robust. Here is what several stackoverflow posts mentioned adding to the listing.rb model:
validates_attachment_content_type :avatar, :content_type => %w(image/jpeg image/jpg image/png)
Unfortunately I still get the same error when I attach an image. When I don't attach an image my default image is loaded fine and the listing is created properly.
My Listing model:
class Listing < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "150x", :thumb => "100x100>" }, :default_url => "default.jpg"
validates_attachment_content_type :avatar, :content_type => %w(image/jpeg image/jpg image/png)
end
My _form.html.erb partial:
<%= form_for @listing, :html => { :multipart => true } do |f| %>
<% if @listing.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@listing.errors.count, "error") %> prohibited this listing from being saved:</h2>
<ul>
<% @listing.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :name %><br>
<%= f.text_field :name, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :company %><br>
<%= f.text_field :company, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :email %><br>
<%= f.text_field :email, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :phone %><br>
<%= f.text_field :phone, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :avatar %><br>
<%= f.file_field :avatar, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
My listings_controller.rb controller:
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
And my schema.rb file
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
EDIT: Adding console output after running $rails generate paperclip listing avatar
(I need 10 reputation points to put in post so you have to settle for link http://i.imgur.com/c8KGTa3.png)
解决方案
I suppose you forgot to create the corresponding fields for avatar
in listings
table.
I would suggest you to generate a migration to add avatar
to listings
table as below:
rails generate paperclip listing avatar
Then run rake db:migrate
UPDATE
As per your comments and EDIT, you have a migration file to add avatar
to listings
table which you created by running rails generate paperclip user avatar
but unfortunately for some reason its not going through i.e., there are no avatar specific fields("avatar_file_name", "avatar_content_type", "avatar_file_size" and "avatar_updated_at")
in listings
table as per your db/schema.rb
. This is a very strange behavior.
I would suggest you to follow the below mentioned steps in order:
Destroy the existing migration, if any:
rails destroy paperclip listing avatar
Generate a new migration:
rails generate paperclip listing avatar
Run
rake db:migrate
UPDATE 2
I hope you did not down voted me (but someone did), so I would like to bring it to notice that it is an ongoing issue with Paperclip and I did suggest a solution in my comments (Mar 31) as below:
I want you to try as gem 'paperclip', :git =>
"git://github.com/thoughtbot/paperclip.git" in Gemfile and then bundle
install. Let me know when you finish
Apparently it wasn't noticed by you or someone who down voted me today.
Also, you said No errors as far as I can tell, image here:
i.imgur.com/c8KGTa3.png BUT if you look at the output there is an error stating clearly:
migration_file_name': protected methodmigration_file_name' called for
PaperclipGenerator:0x007fb3c6494c20 (NoMethodError)
这篇关于回形针错误:模型缺少 'avatar_file_name' 所需的 attr_accessor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文