1

I am making my first steps with the Rails plugin 'paperclip' an therefor watched the RailsCast #134: http://railscasts.com/episodes/134-paperclip

Did everthing the same, except that I'm running rails 3.0.9 and installed paperclip (2.3.15) via adding it to the Gemfile.

Until 3:00 of the cast, everything works fine. But after reloading the show-page, I get the "missing" image instead of the uploaded image. Also, inside of the 'public' directory nothing new has been created.

Any hints?

Update: As requested here the relevant code:

Gemfile:
…
gem 'paperclip'
gem 'rails', '3.0.9'
…

config/routes.rb:
Foobar::Application.routes.draw do
  resources :books
end

app/models/book.rb:
class Book < ActiveRecord::Base
  has_attached_file :cover
  attr_accessor :cover_file_name
end

app/controllers/books_controller.rb:
# nothing changed here after scaffolding

app/views/books/_form.html.erb:
<%= form_for(@book, :html => { :multipart => true}) do |f| %>
…
<div class="field">
  <%= f.file_field :cover %>
</div>

app/views/books/show.html.erb:
…
<%= image_tag «book.cover.url %>
…

db/migrate/..._create_books.rb:
class CreateBooks < ActiveRecord::Migration
  def self.up
    create_table :books do |t|
      t.string :name

      t.timestamps
    end
  end

  def self.down
    drop_table :books
  end
end

db/migrate/..._ad_attachment_cover_to_book.rb:
class AddAttachmentCoverToBook < ActiveRecord::Migration
  def self.up
    add_column :books, :cover_file_name, :string
    add_column :books, :cover_content_type, :string
    add_column :books, :cover_file_size, :integer
    add_column :books, :cover_updated_at, :datetime
  end

  def self.down
    remove_column :books, :cover_file_name
    remove_column :books, :cover_content_type
    remove_column :books, :cover_file_size
    remove_column :books, :cover_updated_at
  end
end

I started up with "rails generate paperclip book cover" after having scaffold "book"

dennis
  • 51
  • 6
  • did you add `:html => {:multipart => true}` in your form? – apneadiving Jul 16 '11 at 16:11
  • Yes, I add the multipart part according to paperclip's install instructions. – dennis Jul 16 '11 at 21:00
  • I created a new app from scratch. This time, the uploaded file gets stored (according to the log and the filesystem), but nevertheless after uploading I get the "missing" image. That's the relevant part of the log: [paperclip] Saving attachments. [paperclip] saving /home/myname/foobar/public/system/covers/4/original/03.png Started GET "/covers/original/missing.png" ActionController::RoutingError (No route matches "/covers/original/missing.png"): Rendered /var/lib/gems/1.8/gems/actionpack-3.0.9/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.9ms) – dennis Jul 16 '11 at 21:04
  • Post your code. You've got something configured/setup wrong and/or you're accessing the URL wrong. – coreyward Jul 17 '11 at 03:04

2 Answers2

2

I think that the attr_accessor :cover_file_name creates a conflict for the table column with the same name. Try removing that line. Can't find anything else that should cause any problems.

DanneManne
  • 21,107
  • 5
  • 57
  • 58
  • Thanks. In the meantime, I found that out, too. While digging deeper, I also found out, that my problem was more on layer 8 :P It seems that I missed to rake db:migrate after scaffolding paperclip, because I found, that the paperclip columns were completely missing in the db. Strange that this happened twice to me (at my regular rails app _and_ at the dummy app that I created for this post). *douh* – dennis Jul 17 '11 at 12:30
  • Hehe, that can happen when you start digging :) – DanneManne Jul 17 '11 at 12:33
  • and another addition: In my regular rails app there was another thing that made it a bit more complicated: I use the plugins devise and cancan. Therefor my model has `attr_accessible` in it. Until now, I had not figured out, that I have to add the attachment field from `has_attached_file` also to that list. Now it works *phew* :) – dennis Jul 17 '11 at 13:02
-1

People I think I already gave the solution when you appear this error is because the cover column does not have permission to enter this is solved in the controller of articles in the params_article can only manipulate the title and the body to this also adds to it To the column cover to me I stay like this

Def articulo_params params.require (: article) .permit (: title,: body,: cover)
End
Aurasphere
  • 3,841
  • 12
  • 44
  • 71