Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
871 views
in Technique[技术] by (71.8m points)

ruby on rails - Paperclip Error: model missing required attr_accessor for 'avatar_file_name'

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...