Active admin multiple file/image upload with paperclip - ruby-on-rails

I use Active admin and I need upload Galleries with a lot of images. How can I do it?
My code:
class Gallery < ActiveRecord::Base
belongs_to :event
has_many :images
attr_accessible :name, :publish, :images, :image, :images_attributes
accepts_nested_attributes_for :images, allow_destroy: true
validates :name, presence: true
end
class Image < ActiveRecord::Base
belongs_to :gallery
attr_accessible :url
has_attached_file :url, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
ActiveAdmin.register Gallery do
form html: { multipart: true } do |f|
f.inputs do
f.input :name
f.input :images, as: :file, input_html: { multiple: true}
end
f.buttons
end
end
And I have this error:
Image(#70319146544460) expected, got ActionDispatch::Http::UploadedFile(#70319105893880)

Try this:
ActiveAdmin.register Gallery do
form multipart: true do |f|
f.inputs do
f.input :name
f.has_many :images do |p|
p.input :url
end
end
f.actions
end
end

Okay, I managed to solve it:
Try to do the following:
ActiveAdmin.register Gallery do
form html: { multipart: true } do |f|
f.inputs do
f.input :name
file_field_tag("gallery_images_url", multiple: true, name: "gallery[gallery_images_attributes][][url]")
end
f.buttons
end
end
I got to that solution by following this blog post: http://www.tkalin.com/blog_posts/multiple-file-upload-with-rails-3-2-paperclip-html5-and-no-javascript

Related

undefined method `event_id' when using a belongs_to/has_many association

I'm creating a site that has events. Each event acts as a gallery and has_many images. Each image belongs_to and event.
I followed the RailsCast #253 CarrierWave gem. When I try to add a new image, it says
undefined method `event_id' for # Image:0x7302438
<%= form_for #image, :html => {:multipart => true} do |f| %>
<%= f.error_messages %>
<%= f.hidden_field :event_id %>
<%= f.label :title %><br />
<%= f.text_field :title %>
Here is my image.rb
class Image < ActiveRecord::Base
attr_accessible :event_id, :title, :image
validates :title, :image, :presence => :true, :uniqueness => :true
belongs_to :event
mount_uploader :image, ImageUploader
end
and the event.rb
class Event < ActiveRecord::Base
attr_accessible :title, :date, :about
validates :title, :about, :date, :presence => :true
validates :title, :uniqueness => :true
has_many :images
extend FriendlyId
friendly_id :title, use: [:slugged, :history]
end
Running this in a migration worked. Column wasn't added.
def up
change_table :images do |t|
t.references :event
end
end

Formtastic nested_form with only one "add new image" upload button

I have a Project model:
class Project < ActiveRecord::Base
attr_accessible :active, :address, :date_begun, :date_ended, :description, :name, :url, :organization_id, :invisible, :zone, :definition, :images, :partners, :external_partners, :partner_ids, :external_partner_ids, :images_attributes
has_many :images, :as => :imageble
accepts_nested_attributes_for :images
end
An Image polymorphic model
class Image < ActiveRecord::Base
attr_accessible :imageble_id, :imageble_type, :order, :data
belongs_to :imageble, :polymorphic => true
has_attached_file :data,
:styles => { :mini => "60x60#", :medium => "300x300>", :thumb => "100x100>" },
...
end
A project controller:
class Admin::ProjectsController < ApplicationController
def edit
#project = Project.find(params[:id])
#project.images.build
#hash_partners = Organization.retrieve_hash(#project.partners.collect{|o| o.id})
#hash_external_partners = ExternalOrganization.retrieve_hash(#project.external_partners.collect{|eo| eo.id})
end
end
And a formtastic form for projects:
<%= semantic_form_for ([:admin, #project]) do |f| %>
<%= f.inputs do %>
<%= f.input :name, :label =>"Nome", :hint => "Clicca sul nome per cambiare", :placeholder => "nome del progetto" %>
<%= f.semantic_fields_for :images do |images| %>
<%= images.inputs :data %>
<% end %>
<% #project.images.each do |i| %>
<%= image_tag i.data(:mini) %>
<% end %>
<% end %>
<%= f.actions %>
<% end %>
Everything is working but as I add images to the project the form renders more and more file input buttons to the form.
What I'd like is to have only one file upload button to add new images to the project.
And then have a list of the project images thumbnails.
How can I achieve this in Rails3 and Formtastic 2.1.1 ?

Ruby, Paperclip, Formtastic, ActiveAdmin and images

How do I create a form in Paperclip, ActiveAdmin, and Formtastic? The image is not part of the model, but part of a sub-model.
I tried:
form :html => {:multipart => true} do |f|
f.inputs "Ticket Info" do
...
f.input :image1.photo
f.input :image2.photo
But it gave an error ActionView::Template::Error (undefined method 'photo' for :image1:Symbol):.
Here is the Ticket model:
class Ticket < ActiveRecord::Base
attr_accessible ... :image1_id, :image2_id
...
belongs_to :image1, class_name: "TicketImage"
belongs_to :image2, class_name: "TicketImage"
Here is the TicketImage model:
class TicketImage < ActiveRecord::Base
attr_accessible :file, :photo
has_one :ticket
has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
end
I also tried f.input :image1 but it just gave me an empty select box.
I also tried f.input :image1, :as => :file, but it gave me this error after I selected a file and clicked submit:
ActiveRecord::AssociationTypeMismatch in Admin::TicketsController#update
TicketImage(#89768376) expected, got ActionDispatch::Http::UploadedFile(#29533068)
I also tried
f.semantic_fields_for :image1 do |image|
image.input :photo, :as => :file, :name => "Image1"
end
f.semantic_fields_for :image2 do |image|
image.input :photo, :as => :file, :name => "Image2"
end
but it only gave one file select button labeled Photo, and after I submitted, gave this error:
ActiveRecord::AssociationTypeMismatch in Admin::TicketsController#update
TicketImage(#86546820) expected, got ActiveSupport::HashWithIndifferentAccess(#20656416)
I also tried this: Added:
ActiveAdmin.register Ticket do
...
f.input :photo, :as => :file, :for => :image1, :name => "Image 1"
class Ticket < ActiveRecord::Base
attr_accessible ... :image1_id, :image2_id, :image1_attributes, ...
accepts_nested_attributes_for :image1, :image2, :allow_destroy => true
class TicketImage < ActiveRecord::Base
attr_accessible :file, :photo, :photo_file_name, :photo_content_type, :photo_file_size, :photo_updated_at
And the file upload box appears and accepts an upload, but in the log, it says:
WARNING: Can't mass-assign protected attributes: photo
I also tried this, which works with ONE upload field, but when both are put in the form, NEITHER are displayed! There are no errors in the server log either:
f.semantic_fields_for :image1 do |image|
image.input :photo, :as => :file, :name => "Image1", :hint => image.object.nil? ? "No Image" : f.template.image_tag(image.object.photo.url(:thumb))
end
# f.semantic_fields_for :image2 do |image|
# image.input :photo, :as => :file, :name => "Image2", :hint => image.object.nil? ? "No Image" : f.template.image_tag(image.object.photo.url(:thumb))
# end
I also tried this, which seems to works all the way and save images, except it allows you to create one-to-many association with the image1 field, which is only a single entry! I haven't tried adding two images to image1, but I expect it to crash.
f.has_many :image1 do |p|
p.input :photo, :as => :file, :label => "Image 1", :hint => p.template.image_tag(p.object.photo.url(:thumb))
p.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove image'
end
f.has_many :image2 do |p|
p.input :photo, :as => :file, :label => "Image 2", :hint => p.template.image_tag(p.object.photo.url(:thumb))
p.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove image'
end
This works the best, but hints don't work!
f.inputs :photo, :as => :file, :for => :image1, :name => "Image 1", :hint => "A Hint"
f.inputs :photo, :as => :file, :for => :image2, :name => "Image 2", :hint => "A Hint"
Nested forms are also supported by Formtastic:
form :html => {:multipart => true} do |f|
f.input :number
f.semantic_fields_for :image1 do |image|
image.input :photo, :name => "Image1"
f.semantic_fields_for :image2 do |image|
image.input :photo, :name => "Image2"

cannot update paperclip image in active admin nested form

I have models product and product images. Product_images is a paperclip model. Product has many product_images. I am making an Active Admin form which uploads multiple images and shows these images to the Products view page.
However, when I save the product. The product table is updated, but not the product_image table. Image attaching normally, but I can't update fields of already uploaded image.
class Product < ActiveRecord::Base
attr_accessible :name, :product_images_attributes
has_many :product_images, :dependent => :destroy
accepts_nested_attributes_for :product_images, :reject_if => lambda { |t| t['product_image'].nil? }, :allow_destroy => true
end
class ProductImage < ActiveRecord::Base
attr_accessible :name, :style
attr_accessible :image
belongs_to :product
has_attached_file :image, :styles => { :small => "150x150>", :large => "320x240>" }
validates_attachment_presence :image
end
ActiveAdmin.register Product do
form :html => { :multipart => true } do |f|
f.inputs "Admin Details" do
f.input :name
end
f.inputs "Product images" do
f.has_many :product_images do |p|
p.input :style, :as => :select, :collection => Image::STYLES, :include_blank => false
p.input :image, :as => :file, :label => "Image",:hint => p.object.image.nil? ? p.template.content_tag(:span, "No Image Yet") : p.template.image_tag(p.object.image.url(:small))
p.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove image'
end
end
f.buttons
end
UPDATE
In the products model I do:
after_update :check
def check
if ProductImage.find_by_product_id(self.id).changed?
raise "image"
else
raise "fail"
end
end
and it's alway raise "fail"
I got the same error using rails 4.1. Just solved this.
I noticed that warnings in my output:
Unpermitted parameters: _destroy, id
Unpermitted parameters: _destroy, id
Unpermitted parameters: _destroy, id
So I just passed them to permit_params:
permit_params assets_attributes: [:image, :image_file_name, :image_content_type, :image_file_size, :image_updated_at, :_destroy, :id]
to AA's resource page. And it worked.
I hope it will help.
If not - here is my listings.
ActiveAdmin.register Product do
permit_params :name, :description, :price, :brand_id, :category_id, assets_attributes: [:image, :image_file_name, :image_content_type, :image_file_size, :image_updated_at, :_destroy, :id]
form multipart: true do |f|
f.inputs "Детали" do
f.input :category_id, as: :select, collection: Category.all, include_blank: false
f.input :brand_id, as: :select, collection: Brand.all, include_blank: false
f.input :name
f.input :description
f.input :price
end
f.inputs 'Фотографии' do
f.has_many :assets, allow_destroy: true, heading: 'Фото', new_record: false do |fasset|
fasset.input :image, as: :file, hint: fasset.template.image_tag(fasset.object.image.url(:thumb))
end
end
f.actions
end
end
class Product < ActiveRecord::Base
belongs_to :category
belongs_to :brand
has_many :assets, dependent: :destroy, autosave: true
accepts_nested_attributes_for :assets, allow_destroy: true,
:reject_if => lambda { |attributes| attributes[:image].blank? }
validate :name, presence: true
validate :description, presence: true
validate :price, presence: true
end
class Asset < ActiveRecord::Base
belongs_to :product
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
validates_with AttachmentPresenceValidator, :attributes => :image
end
You can configure ActiveRecord to cascade-save changes to items in a collection for a model by adding the :autosave => true option when declaring the association.
Try:
has_many :product_images, :dependent => :destroy, :autosave => true
Also, you can save the table data on association in the after_save callback like this:
class Product < ActiveRecord::Base
attr_accessible :name, :product_images_attributes
has_many :product_images, :dependent => :destroy
accepts_nested_attributes_for :product_images, :reject_if => lambda { |t| t['product_image'].nil? }, :allow_destroy => true
after_save :do_product_image_update
private
def do_product_image_update
self.product_images.save
end
end

File upload with Activeadmin Rails using paperclip

I use Active admin as my rails application backend. I want to make a file upload. How can I accomplish this functionality?
I found a way to use Paperclip with Active Admin.
I added this code in my model "Event" :
has_attached_file :map, :styles => { :medium => "238x238>",
:thumb => "100x100>"
}
And i did this for my admin model :
ActiveAdmin.register Event do
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Details" do
f.input :continent
f.input :event_type
f.input :name
f.input :title
f.input :content
f.input :date_start, :as => :date
f.input :date_end, :as => :date
f.input :place
f.input :map, :as => :file
f.input :image, :as => :file, :hint => f.template.image_tag(f.object.image.url(:medium))
f.input :userfull_info
f.input :price
f.input :phone, :as => :phone
f.input :website, :as => :url
end
f.buttons
end
end
To use it on the index page, you have to use :
column "Image" do |event|
link_to(image_tag(event.image.url(:thumb), :height => '100'), admin_event_path(event))
end
default_actions
end
Got it worked for Rails 4.1 and Paperclip 4.1:
Model
class Hotel < ActiveRecord::Base
has_attached_file :thumbnail, :styles => { :medium => "300x300#", :thumb => "200x200#" }
validates_attachment :thumbnail, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png"] }
end
Admin Model
ActiveAdmin.register Hotel do
permit_params :name, :description, :price, :thumbnail
form do |f|
f.inputs "Project Details" do
f.input :name
f.input :thumbnail, :required => false, :as => :file
# Will preview the image when the object is edited
end
f.actions
end
show do |ad|
attributes_table do
row :name
row :thumbnail do
image_tag(ad.thumbnail.url(:thumb))
end
# Will display the image on show object page
end
end
end
I'm using the rails 3.0.1 and the following code
f.input :image, :hint => "current image: #{f.template.image_tag(f.object.image.url(:thumb))}"
return a string. After search a solution, i found it.
f.input :image, :hint => f.template.image_tag(f.object.image.url(:thumb))
Send direct the object, will return a image to the html
In latest Version of ActiveAdmin & Rails 6 for displaying the file field we need to use the below code
ActiveAdmin.register Project do
permit_params :name, :uploads
form multipart: true do |f|
f.inputs "Project Details" do
f.input :name
f.input :uploads, as: :file, required: false
end
f.actions
end
end
In some old version of AA following code also worked.
f.input :uploads, required: false

Resources