Paperclip错误:'avatar_file_name'需要attr_accessor所需的模型 [英] Paperclip Error: model missing required attr_accessor for 'avatar_file_name'

查看:141
本文介绍了Paperclip错误:'avatar_file_name'需要attr_accessor所需的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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'

listing_controller的第44行:

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

要尝试的一些事项:即在listing.rb模型中添加一些代码以使其可接受图像为:头像更健壮。以下是几个stackoverflow帖子中提到的添加到listing.rb模型的内容:

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.

我的上市模型:

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 partial:

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 %>

我的listings_controller.rb控制器:

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

我的schema.rb文件

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

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

Adding console output after running $rails generate paperclip listing avatar

(我需要10个声望点才能发布,所以你必须解决链接 http://i.imgur.com/c8KGTa3.png

(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.

我建议你生成一个迁移,将 avatar 添加到 listing 表格如下:

I would suggest you to generate a migration to add avatar to listings table as below:

rails generate paperclip listing avatar

然后运行 rake db:migrate

根据您的意见和编辑,你有一个迁移文件将 avatar 添加到 listing 表,你通过运行 rails generate创建了这个表回形针用户头像但遗憾的是由于某种原因它没有经过ie,没有头像特定字段(avatar_file_name,avatar_content_type,avatar_file_size和avatar_updated_at) listing 表中根据您的 db / schema.rb 。这是一种非常奇怪的行为。

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  

生成新迁移:

rails generate paperclip listing avatar

运行

rake db:migrate



UPDATE 2



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

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:


我希望你尝试宝石'paperclip',:git =>
gm://github.com/thoughtbot/paperclip.git在Gemfile中然后捆绑
install。完成后告诉我

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 但如果您查看输出,则会出现错误说明:

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'调用
PaperclipGenerator:0x007fb3c6494c20(NoMethodError)

migration_file_name': protected methodmigration_file_name' called for PaperclipGenerator:0x007fb3c6494c20 (NoMethodError)

这篇关于Paperclip错误:'avatar_file_name'需要attr_accessor所需的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆