Thibaud Clement
Thibaud Clement

Reputation: 6897

Review model missing required attr_accessor for 'poster_file_name'

I am building a very simple app that lets users post reviews and comment upon them, with the following models:

There is no authentication system.

So far, the app is working.

Now, I am trying to add a feature that will let users upload an image (the movie poster) when adding a review.

So, following this tutorial:

I installed the latest versions of ImageMagik and Paperclip,

I added the following to my review model:

class Review < ActiveRecord::Base
  has_attached_file :poster, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :poster, :content_type => /\Aimage\/.*\Z/
end

I ran the following migration:

rails generate paperclip review poster

which displayed this in my Terminal:

MacBook-Pro-de-Thibaud-CLEMENT:moview Thibaud$ rails generate paperclip review poster
/Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.0.5/lib/rails/generators/actions/create_migration.rb:13:in `migration_file_name': protected method `migration_file_name' called for #<PaperclipGenerator:0x007fac51501428> (NoMethodError)
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.0.5/lib/rails/generators/actions/create_migration.rb:34:in `existing_migration'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/thor-0.19.1/lib/thor/actions/empty_directory.rb:112:in `invoke_with_conflict_check'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/thor-0.19.1/lib/thor/actions/create_file.rb:60:in `invoke!'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/thor-0.19.1/lib/thor/actions.rb:94:in `action'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.0.5/lib/rails/generators/migration.rb:36:in `create_migration'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.0.5/lib/rails/generators/migration.rb:65:in `migration_template'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/paperclip-4.1.1/lib/generators/paperclip/paperclip_generator.rb:16:in `generate_migration'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `block in invoke_all'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `each'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `map'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `invoke_all'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/thor-0.19.1/lib/thor/group.rb:232:in `dispatch'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.0.5/lib/rails/generators.rb:156:in `invoke'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.0.5/lib/rails/commands/generate.rb:11:in `<top (required)>'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:229:in `require'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:229:in `block in require'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:214:in `load_dependency'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:229:in `require'
    from /Users/Thibaud/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.0.5/lib/rails/commands.rb:48:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

and then ran:

rake db:migrate

I did stop and restart my server.

I edited my Review#Edit view files with the following line:

<%= form.file_field :poster %>

I update my review controller:

def review_params
      params.require(:review).permit(:title, :poster, :content, :user_id)
    end

And I added

<%= image_tag @review.poster.url(:medium) %>

to both the Review#Index and the Review#Show

When I visit the Review#Index page, the app works.

When I visit the Review#Edit page, the app works too: it displays a field to upload an image.

However, as soon as I click the "Update review" button, then, I get the following error message:

enter image description here

How can I make this work?

Upvotes: 0

Views: 189

Answers (2)

user740584
user740584

Reputation:

As already stated the protected_method error generating a Paperclip migration is a known issue, targetted for inclusion in version 4.1.2.

If you prefer not to modify your Gemfile and so avoid any other edge changes which might cause side-effects, you can simply create your own migration manually:

class AddPosterColumnsToReviews < ActiveRecord::Migration
  def self.up
    add_attachment :reviews, :poster
  end

  def self.down
    remove_attachment :reviews, :poster
  end
end

This is exactly what the generator would do. After creating it, just run rake db:migrate as normal.

Upvotes: 1

Yann VERY
Yann VERY

Reputation: 1819

The paperclip migration generator failed, it's a known issue on paperclip version 4.1: https://github.com/thoughtbot/paperclip/issues/1495

Try to update your gemfile with :

gem 'paperclip', github: 'thoughtbot/paperclip'

after that make :

bundle install
rails generate paperclip review poster
rake db:migrate`

that will be ok.

Upvotes: 1

Related Questions