Search code examples
ruby-on-railsactivemodel

Can't mass-assign protected attributes: image


I'm new to rails and just cannot seem to find the issue. I've been googling for a while now and searching on here. I think i'm just missing something really simple that im not catching.

The error message I'm getting is.

"Can't mass-assign protected attributes: image"

The error according to 'Applications Frames' is in the photos controller here around line 13.

8     @photo = Photo.new
9     @gallery = Gallery.find(params[:gallery_id])
10   end
11 
12   def create
13     @photo = Photo.new(params[:photo])
14 
15     if @photo.save
16       flash[:notice] = "photo uploaded."
17       redirect_to galleries_url
18     else

In my photo.rb page I have the following:

class Photo < ActiveRecord::Base

    attr_accessible :name, :photo

    has_many :photos
    validates_presence_of :title
    validates_uniqueness_of :title

    belongs_to :gallery

end

Can someone help point me in the right direction?

Thanks!


Solution

  • params[:photo] includes an :image key.

    Photo.new({ hash of things including an :image key }) is trying to set the :image attribute on a new Photo.

    Assuming there even is an :image attribute for that model, you'll need to set attr_accessible :image on your model in order for Rails to let you set :image in any operation where you pass it a hash of attributes.

    See the Rails attribute security guide for background info.