Nested Form does not Display form for ActiveAdmin - ruby-on-rails

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.

Related

How to set value of current logged-in user

I'm creating an active admin form where I want to set the value of "admin_user_id" to be the current user logging in and want to hide this as we don't need to show while filling the form. here is my form for active admin:
form do |f|
f.inputs do
f.input :admin_user_id, input_html: { value: current_admin_user }
f.input :title
f.input :description
end
f.actions
end
I resolved the issue by replacing "current_admin_user" with "current_admin_user.id".
form do |f|
f.inputs do
f.input :admin_user_id, input_html: { value: current_admin_user.id }
f.input :title
f.input :description
end
f.actions
end

ActiveAdmin with form of association opened in default

How to turn the form auto opened without the needs of click in button?
From this button:
https://i.stack.imgur.com/zXOec.png
To this form:
https://i.stack.imgur.com/IRpAD.png
My code is like this:
form do |f|
f.inputs "User" do
f.input :name
f.has_many :addresses, allow_destroy: true do |b|
b.input :street
b.input :number
b.input :complement
end
end
end

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

upload multiple images at a time with paperclip using activeadmin

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

Resources