Rails activeadmin/carrierwave has_many photos relationship throwing mass-assignment exception

China☆狼群 提交于 2019-12-13 02:56:14

问题


I have a model BlogPost defined as

class BlogPost < ActiveRecord::Base

  attr_accessible :title, :body, :photo, :photos_attributes, as: :admin
  has_many :photos, class_name: 'BlogPhoto', dependent: :destroy
  validates :body, :photo, :title, presence: true
  validates :title, uniqueness: true
  accepts_nested_attributes_for :photos, allow_destroy: true
end

from schema

create_table "blog_posts", :force => true do |t|
    t.string   "title"
    t.text     "body"
    t.datetime "created_at", :null => false
    t.string   "photo"
    t.datetime "updated_at", :null => false
end
class BlogPhoto < ActiveRecord::Base
  belongs_to :blog_post
  attr_accessible :photo, as: :admin

  mount_uploader :photo, BlogPhotoUploader
end

and an activeadmin form to create a new blogpost

form do |f|
    f.inputs "Blog Post" do
      f.input :title
      f.input :body, as: :html_editor
    end
    f.inputs "Photos" do
      f.has_many :photos do |p|
        p.input :photo, as: :file
        p.input :_destroy, as: :boolean, required: false, label: 'Remove Photo'
      end
    end
    f.actions
 end

I am also using carrierwave to upload photos to S3. With all of thise code, when I try to create a new blogpost inside of active admin, I get a mass-assignment error saying: Can't mass-assign protected attributes: title, body, photos_attributes

However, you can see I've very clearly white-listed those attributes, so what could be causing this?

EDIT: Included following line to

class BlogPost < ActiveRecord::Base
  mount_uploader :photo, BlogPhotoUploader

and now the exception is

undefined method `validate_integrity' for :BlogImageUploader:Symbol

来源:https://stackoverflow.com/questions/25614802/rails-activeadmin-carrierwave-has-many-photos-relationship-throwing-mass-assignm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!