upload multiple images at a time with paperclip using activeadmin - ruby-on-rails

i'm trying to allow myself to upload 5 images for each model using active admin but i can't seem to figure out how to do it. here is my activeadmin code so far:
ActiveAdmin.register Piece do
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Details" do
f.input :name
f.input :description
f.input :cost
f.input :category
f.input :photo, :as => :file, :hint => f.template.image_tag(f.object.photo.url(:medium))
end
f.buttons
end
index do
column :id
column :name
column :cost
column :category
column :inventory_count
column :available_count
column :materials
column :created_at
default_actions
end
end
how would I allow for 5 at a time instead of just 1?

Here's how it'd work:
class Piece
has_many :pictures
accepts_nested_attributes_for :pictures
end
class Picture
has_attached_file :photo
end
Then in your active admin form, you'd
f.has_many :pictures do |ff|
ff.input :photo, as: :file, :hint => ff.template.image_tag(ff.object.photo.thumb.url)
ff.input :_destroy, as: :boolean
end

Related

Nested Form does not Display form for ActiveAdmin

I am current using activeadmin to create a nested form. I can click on the add nested resource button which will display the inputs for the respective nested resources. However, I would like to have the inputs display by default without having to click on the add resource button (i.e the inputs should be displayed once the form is loaded).
I have rechecked my code against the ActiveAdmin docs and also checked various stackoverflow posts but was unable to make much progress.
Picture of how the form current looks like
My code is as follows in admin/project.rb:
form title: 'Create a New Project' do |f|
f.inputs 'Basic Information' do
f.input :category
f.input :name
f.input :location
f.input :size, sortable: :size do |project|
"#{project.size}m2"
end
f.input :published?
f.has_many :project_main_image, heading: 'Project main image', allow_destroy: true, new_record: false do |a|
a.input :photo, hint: image_tag(a.object.photo.thumb.url, class: 'active-admin-thumbnail')
a.input :orientation
end
f.has_many :project_descriptions, allow_destroy: true do |a|
a.input :contents, as: :ckeditor, input_html: { ckeditor: { toolbar: 'Full' } }
end
f.has_many :project_gallery_images, allow_destroy: true do |a|
a.input :photo, hint: image_tag(a.object.photo.thumb.url, class: 'active-admin-thumbnail')
a.input :orientation
a.input :row_order
end
f.actions
end
end
Any feedback or advice. Do let me know if you require more information.
That's what something i did.
you can take nested attributes like this with for keyword:
form do |f|
f.inputs do
f.input :user, input_html: { disabled: true }
f.input :name
f.input :address
f.input :city
f.input :country, as: :string
end
f.buttons
f.inputs "Account Information", for: [:account, f.object.account] do |s|
s.input :active, as: :boolean
s.input :subscription, as: :boolean
s.input :expires_on, as: :datepicker
s.actions
end
end
controller do
def permitted_params
params.permit!
end
end
end
Hope this is helpful.

How can I use Active Admin's form block to update a table that tracks when an attribute in a resource is updated?

How can I use Active Admin's form block to update a table that tracks when a particular attribute in the current resource is updated?
For instance, I have something like this:
form do |f|
f.inputs "Details" do
f.input :name
f.input :address
f.input :zip
f.input :city
f.input :active_state, as: :select, collection: active_states
f.input :approval_state, as: :select, collection: approval_states
f.input :new_comment
end
f.buttons
end
where everything but new_comment is an attribute that exists on the current resource.
I tried doing something like this in the class for the resource:
def new_comment
history = History.new(:id, Time.now)
history.comment
end
I also tried creating a controller instance variable when that failed, but that didn't work either. The venue class doesn't really have any co-relation to the history class. I'm just trying to create an input column that will create a new row in my History table whenever the user edits a venue and then adds a comment.
The History model looks like this:
class History < ActiveRecord::Base
attr_accessible :comment, :venue_id, :created_at
belongs_to :venue
def initialize(id, datetime)
#id = id
#datetime = datetime
end
end
The form is for a Venue that looks like this:
class Venue < ActiveRecord::Base
attr_accessible :name, :address, :zip, :city
has_many :histories
accepts_nested_attributes_for :histories
//with a bunch of methods
end
I tried doing this after some digging but it didn't work either:
form do |f|
f.inputs "Details" do
f.input :name
f.input :address
f.input :zip
f.input :city
f.input :active_state, as: :select, collection: active_states
f.input :approval_state, as: :select, collection: approval_states
end
f.inputs "History" do
f.has_many :histories do |h|
h.input :id
h.input :comment
h.input :modified_at
end
end
f.buttons
end
In addition to setting up the model relationships and accepts_nested_attributes_for, try this in your ActiveAdmin resource:
form do |f|
f.inputs "Details" do
f.input :name
f.input :address
f.input :zip
f.input :city
f.input :active_state, as: :select, collection: active_states
f.input :approval_state, as: :select, collection: approval_states
f.has_many :histories do |h|
h.input :id
h.input :comment
h.input :modified_at
end
end
f.actions
end

Activeadmin ajaxing form with geocoder

I have a model relation of:
event -> has many schedules
schedule -> has one address
I wanted to have a extra textfield in the address form which allows user to enter an address and click 'search button', so that user can ajax select the geocoder addresses.
however, I can't seem to create an custom(non model related) textfield or button
form do |f|
f.inputs do
f.input :title
f.input :category
f.input :avatar, :as => :file
f.input :description, as: :html_editor
#schedules
f.has_many :schedules do |schedule|
schedule.inputs "Schedules" do
link_to("Auto Fill Address", find_location_path)
#address
schedule.inputs "Address", :for => [:address, schedule.object.address || Address.new] do |address|
address.input :full_address
address.input :city
address.input :country, :include_blank => true
address.input :postcode
address.input :latitude
address.input :longitude
end
#schedule times
schedule.input :start_time, :as => :just_datetime_picker
schedule.input :end_time, :as => :just_datetime_picker
end
end
end
f.buttons
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

Help with Formtastic

First of all I'm no native speaker and just begun with rails three days ago. Sorry for my mistakes.
Formtastic is driving me crazy. I have three tables: user, note, receiver:
class User < ActiveRecord::Base
has_many :receivers
has_many :notes, :through => :receivers
attr_accessible :id, :email, :password, :password_confirmation, :remember_me
class Note < ActiveRecord::Base
has_many :receivers
has_many :users, :through => :receivers
attr_accessible :id, :text, :user_id
accepts_nested_attributes_for :receivers
class Receiver < ActiveRecord::Base
belongs_to :user
belongs_to :note
attr_accessible :user_id, :note_id, :note_attributes
accepts_nested_attributes_for :user
accepts_nested_attributes_for :note
And here my formtastic form:
<%= semantic_form_for #note do |form| %>
<%= form.inputs do %>
<%= form.input :text %>
<%= form.input :user_id, :as => :check_boxes, :collection => User.find(:all, :conditions => ["id != ?", current_user.id], :order => 'id').collect{|u| [u.email, u.id]} %>
<% end %>
<%= form.buttons %>
<% end %>
Now I want to create a new note which can have several receivers. Unfortunately only the note is created, but no entrys in the receiver table, even if I select receivers. Can someone help me please?
Here my notes_controller:
#note = Note.new(params[:note])
Print out the params[:note] using logger.info and check what all parameters are passed from form and Can you also try adding code reciever_ids code as attr_accessible in Note model
In the view model, you are using attr_accessible, it wont save any fields that are not in the attr_accessible like the receives_attributes, that comes from the form when your nested form is displayed. So you have to add receiver_attributes to the attr_accessible list.You might want to do this to the User and Receiver(if you are having nested forms for them too), which also have attr_accessible
attr_accessible :id, :text, :user_id, :receiver_attributes
In the new action of notes_controller, you need to use build method like
#note.build_receiver
then in the form, you need to write the code to display the fields in the receiver.
<%= semantic_form_for #note do |form| %>
<%= form.inputs do %>
<%= form.input :text %>
<%= form.input :user_id, :as => :check_boxes, :collection => User.find(:all, :conditions => ["id != ?", current_user.id], :order => 'id').collect{|u| [u.email, u.id]} %>
<% end %>
<%=f.semantic_fields_for :receiver_attributes, #note.receiver do|receiver| %>
<!-- Add receiver related input here using the receiver block variable like receiver.input -->
<% end %>
<%= form.buttons %>
<% end %>

Resources