Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paperclip + Active_admin + Rails 3.0.10 multiple images

I have a crud with paperclip and multiple images' i implement active_admin and the product update fine, but, i can't not upload or edit the multiple images, the form i have is this:

form :html => { :multipart => true } do |f|
  f.inputs "Details" do
    f.input :name
    f.input :created_at, :label => "Publish Product at"
    f.input :category
  end

  f.inputs "Images" do
    f.has_many :assets do |p|
      p.input :asset, :as => :file, :input_html => { :multiple => true }, :label => "Image", :hint => p.object.asset.nil? ? p.template.content_tag(:span, "No Image Yet") : p.template.image_tag(p.object.asset.url(:thumb))
      p.input :_destroy, :as=>:boolean, :required => false, :label=>'Remove'
    end
  end

  f.inputs "Content" do
    f.input :description
  end
  f.buttons
end

and the...

f.inputs "Images" do
    f.has_many :assets do |p|
      p.input :asset, :as => :file, :input_html => { :multiple => true }, :label => "Image", :hint => p.object.asset.nil? ? p.template.content_tag(:span, "No Image Yet") : p.template.image_tag(p.object.asset.url(:thumb))
      p.input :_destroy, :as=>:boolean, :required => false, :label=>'Remove'
    end
  end

I want to upload images, but when i create a new asset this have a missing image default and not attach the correct image, i think because the path from the images is not correct to upload. My asset model is:

 class Asset < ActiveRecord::Base
   belongs_to :product
   has_attached_file :asset, :styles => { :large => "340x330", :medium => "140x80>", :thumb => "70x70>" },
     :url => "/products/:id/:style/:basename.:extension",  
     :path => ":rails_root/public/products/:id/:style/:basename.:extension"
 end

how i can modify my assets form to work like i want? Thank you!

like image 771
Stanmx Avatar asked Nov 02 '11 00:11

Stanmx


1 Answers

The Solution

Hi, here is the solution, the key are how work the nested attributes in formtastic...

 form :html => { :multipart => true } do |f|
   f.inputs "Product information" do
     f.input :name
     f.input :description
   end

   f.inputs "Product images" do
     f.has_many :assets do |p|
       p.input :asset, :as => :file, :label => "Image",:hint => p.object.asset.nil? ? p.template.content_tag(:span, "No Image Yet") : p.template.image_tag(p.object.asset.url(:thumb))
       p.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove image'
     end 
   end

   f.inputs "Product details" do
     f.input :category, :label => "Category", :hint => "Select one category"
     f.input :height
     f.input :width
     f.input :depth
     f.input :color, :label => "Color", :hint => "Select one color"
     f.input :sku, :label => "SKU"
     f.input :price
   end
   f.buttons
 end
like image 148
Stanmx Avatar answered Oct 23 '22 00:10

Stanmx