31
30

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rail 4.1でActiveAdminとhas_manyなnested form

Last updated at Posted at 2014-03-11

画像アップロードがCarrierwaveなのは使ってるのに読み替えてもらえれば。

Gemfile
# gem 'formtastic' , github: 'justinfrench/formtastic'
# gem 'ransack'    , github: 'activerecord-hackery/ransack', branch: 'rails-4.1'
# gem 'polyamorous', github: 'activerecord-hackery/polyamorous'
# 上の3つは最近のバージョンではいらなくなったね!
gem 'activeadmin', github: 'gregbell/active_admin'
gem 'carrierwave'
app/models/post.rb
def Post < ActiveRecord::Base
	has_many :images, dependent: :destroy
	accepts_nested_attributes_for :images, allow_destroy: true
end
app/models/image.rb
def Image < ActiveRecord::Base
	belongs_to :post
	mount_uploader :image, ImageUploader
end
app/admin/post.rb
ActiveAdmin.register Post do
	permit_params :title, :body, :images_attributes => [:image, :_destroy, :id]

	form do |f|
    	f.inputs do
			f.input :title
			f.input :body
		end

		f.inputs do
			f.has_many :images, heading: 'Images', allow_destroy: true, new_record: true do |a|
			a.input :image, :hint => a.object.image.nil? ? a.template.content_tag(:span, "no image yet") : a.template.image_tag(a.object.image.thumb.url)
      end
    end

    f.actions
  end
31
30
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
30

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?