Skip to content

Instantly share code, notes, and snippets.

@SonVu
Created August 19, 2014 13:48
Show Gist options
  • Select an option

  • Save SonVu/91b204c032f4fde53108 to your computer and use it in GitHub Desktop.

Select an option

Save SonVu/91b204c032f4fde53108 to your computer and use it in GitHub Desktop.
_form.html.erb (Product)
<% f.fields_for :product_images do |builder| %>
<%= builder.text_field :image %>
<% end %>
-----------
class Product < ActiveRecord::Base
has_many :product_images, dependent: :destroy
accepts_nested_attributes_for :product_images
end
-----------
class ProductImage < ActiveRecord::Base
has_attached_file :image, :stytles => {:medium => "250x250", :thumb => "140x140"}, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
belongs_to :product
end
-----------
ProductController
def new
@product = Product.new
3.times {@product.product_images.build}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment