【问题标题】:Paperclip Error: model missing required attr_accessor for 'avatar_file_name'回形针错误:模型缺少“avatar_file_name”所需的 attr_accessor
【发布时间】:2014-03-30 16:17:20
【问题描述】:

然后我想使用 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'

listings_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 的可接受图像更加健壮。以下是一些 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 %>

我的 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://i.imgur.com/c8KGTa3.png

【问题讨论】:

  • 您是否在listing_params 中包含 :avatar 作为允许的参数?
  • 这确实应该是他的问题。在 def listing_params 中,您至少应该有以下内容: params.require(:listing).permit(:avatar, :avatar_file_name)
  • @rails4guides.com,实际上你不应该在参数中需要:avatar_file_name,只要:avatar 就足够了,因为Paperclip 自己处理它自己的属性。或者换一种说法,我不需要在我的应用中指定它。
  • 你应该这么想。但是,我确实记得曾经看过一个 github 问题,它实际上似乎是必要的。
  • @JJThaeler 我想你忘了在listings 表中创建avatar 的对应字段。

标签: ruby-on-rails ruby image ruby-on-rails-4 paperclip


【解决方案1】:

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

我建议您生成迁移以将avatar 添加到listings 表,如下所示:

rails generate paperclip listing avatar

然后运行rake db:migrate

更新

根据您的 cmets 和 EDIT,您有一个迁移文件可将 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 表中。这是一种非常奇怪的行为。

我建议您按顺序执行以下步骤:

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

rails destroy paperclip listing avatar  

生成新的迁移:

rails generate paperclip listing avatar

运行

rake db:migrate

更新 2

我希望你没有对我投反对票(但有人投了反对票),所以我想提醒一下,这是 Paperclip 的一个持续问题,我确实在我的 cmets(3 月 31 日)中提出了如下解决方案:

我想让你试试 gem 'paperclip', :git => Gemfile 中的“git://github.com/thoughtbot/paperclip.git”,然后捆绑 安装。完成后告诉我

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

migration_file_name':受保护的方法migration_file_name' 要求 PaperclipGenerator:0x007fb3c6494c20 (NoMethodError)

【讨论】:

  • 我确实这样做了 - 请参阅上面的评论。
  • 但是您是否运行了迁移?检查db/schema.rb 文件中listings 表的字段。它应该有 4 个字段,分别名为“avatar_file_name”、“avatar_content_type”、“avatar_file_size”和“avatar_updated_at”。如果它们不存在,则说明您没有运行迁移。
  • 是的,rake 运行了几次。上面我添加了我的 schema.rb 文件。
  • 显然您没有回形针特定字段。请参阅我的更新答案以解决问题。
  • @KirtiThorat 没关系,但我仍然说这不是必需的,我怀疑头像属性存在但在用户表中,因为他通过指向用户模型类运行回形针头像的轨道生成器。
【解决方案2】:

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

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

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

这对我有用,所以希望它对你们中的一些人也有帮助!

【讨论】:

    【解决方案3】:

    确保在 paperclip_database:migration 中使用复数作为新表名,并在回形针生成器中使用单数:

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

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

    【讨论】:

      猜你喜欢
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多