Search code examples
ruby-on-railspaperclipactiveadminformtastic

Active Admin Rails 4 has many


I am working with Rails 4, Active Admin and Paperclip to setup a has_many images association. When generating my has_many portion of the form I keep getting errors. Currently I am getting undefined method `+' for nil:NilClass. Here is my code:

news model

class News < ActiveRecord::Base
    validates :body, presence: true
    validates :title, presence: true, length: { maximum: 140 }

    has_many :news_images, dependent: :destroy
end

News image model

class NewsImage < ActiveRecord::Base
    belongs_to :news



    has_attached_file :photo, styles: {
        small: "150x150>",
        medium: "300x300>",
        large: "600x600>"
    }
    validates_attachment_presence :photo
    validates_attachment_size :photo, less_than: 5.megabytes
end

admin code

ActiveAdmin.register News do
    index do
    column :title
    default_actions
  end

  form multipart: true do |f|
    f.semantic_errors *f.object.errors.keys

    f.inputs "News Details" do
      f.input :title
      f.input :body, :as => :rich
    end

    f.has_many :news_images do |p|

    end

    f.actions
  end

  controller do
    def permitted_params
      params.permit news: [:title, :body, news_images: [:photo]]
    end
  end
end

Ideally I would like the user to be able to upload multiple images to the form. Any one have experience with this issue?

The stack trace is saying insert_tag renderer_for(:new) which is being tripped on f.has_many :news_images do |p|


Solution

  • So the problem was with the news model. I thought accepts_nested_attributes_for was deprecated with the addition of strong params but I guess I was wrong adding this to the news model fixed my issue

    accepts_nested_attributes_for :news_images,
                                :reject_if => lambda { |attributes| attributes[:photo].blank? },
                                :allow_destroy => true