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模型中,以使可接受的图像为:头像更加健壮.这是几个stackoverflow帖子提到添加到listing.rb模型:
validates_attachment_content_type :avatar,:content_type => %w(image/jpeg image/jpg image/png)
不幸的是,当我附加图像时,我仍然会收到相同的错误.当我没有附加图像时,我的默认图像加载正常,并且列表正确创建.
我的房源编号:
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
我的_form.html.erb部分:
<%= 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 %>
我的listing_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
(我需要10个信誉点放在后,所以你必须解决链接http://i.imgur.com/c8KGTa3.png)
解决方法
rails generate paperclip listing avatar
然后运行rake db:migrate
UPDATE
根据您的评论和编辑,您有一个迁移文件添加头像到您创建的列表表,您通过运行rails生成paperclip用户头像,但不幸的是由于某种原因,它不经过ie,没有头像特定的字段(“avatar_file_name”,根据您的db / schema.rb,列表表中的“avatar_content_type”,“avatar_file_size”和“avatar_updated_at”).这是一个非常奇怪的行为.
我建议你按照以下步骤按顺序:
破坏现有迁移(如果有):
rails destroy paperclip listing avatar
生成新的迁移:
rails generate paperclip listing avatar
跑
rake db:migrate
更新2
我希望你没有投票给我(但有人做了),所以我想提醒一下,这是PaperClip的一个持续的问题,我在我的评论(3月31日)中提出了一个解决方案如下:
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
显然,没有注意到你或某人谁今天投票给我.
另外,你说没有错误,据我所知,图像在这里:i.imgur.com/c8KGTa3.png但是如果你看输出有一个错误说明清楚:
migration_file_name’: protected methodmigration_file_name’ called for PaperclipGenerator:0x007fb3c6494c20 (NoMethodError)