Required attractor model attr_accessor is missing with Paperclip

I am trying to load more than one file into a movie. I keep getting the error Asset model missing required attr_accessor for 'asset_file_name'

and I'm not sure why, here is my code:

asset.rb model

class Asset < ActiveRecord::Base
  belongs_to :movie

  has_attached_file :asset
end

      

movie.rb model

class Movie < ActiveRecord::Base
  belongs_to :user

  has_many :assets
  accepts_nested_attributes_for :assets

  validates :title, presence: true

  default_scope order: 'movies.created_at DESC'
end

      

I also ran rails g paperclip movie asset

which generated the following migration file

class AddAttachmentAssetToMovie < ActiveRecord::Migration
  def self.up
    add_column :movies, :asset_file_name, :string
    add_column :movies, :asset_content_type, :string
    add_column :movies, :asset_file_size, :integer
    add_column :movies, :asset_updated_at, :datetime
  end

  def self.down
    remove_column :movies, :asset_file_name
    remove_column :movies, :asset_content_type
    remove_column :movies, :asset_file_size
    remove_column :movies, :asset_updated_at
  end
end

      

And here is my "create assets" migration that came up after creating the asset model:

class CreateAssets < ActiveRecord::Migration
  def change
    create_table :assets do |t|
    t.string :asset_file_name
    t.integer :asset_file_size
    t.string :asset_content_type
    t.datetime :asset_updated_at
    t.text :asset_description
    t.integer :movie_id

    t.timestamps
  end
end

      

Can you guess why I am getting the error Asset model missing required attr_accessor for 'asset_file_name'

?

+3


source to share


1 answer


Newer versions of Rails are (correctly) paranoid about mass assignment. Add attr_accessible :asset_file_name

Asset to Models to declare that it is ok for your database to accept external parameter input when requested.



To be even more secure, uncomment config.active_record.whitelist_attributes = true

in your application.rb

(and then check everything) so that you are forced to think "yes, this is great and nothing bad will happen" for any attributes that you have allowed users to update.

+8


source







All Articles