I looked at this question and was able to get set up a has_many form properly.
Here's where I'm at:
f.has_many :related_contents do |rc|
rc.inputs "first configuration" do
#...
end
end
f.has_many :related_contents do |rc|
rc.inputs "second configuration do
#...
end
end
so I want to have different configurations of fields. What I can't figure out is how to set the <h3> that activeadmin generates and sets as the title for the nested fields. I thought something like
f.has_many :related_contents, :title => "first set" do |rc|
would work, but It does not. Does anybody know what I need to do to get this working right?
I assume that changing the <h3> will also change the button that gets generated to add. Is there another option I need to set for that as well?
Add a :heading option to the f.has_many method
f.has_many :related_contents, :heading => "first set" do |rc|
#...
end
To remove the heading altogether, set it to false or nil
$('h3:contains("Related Contents")').hide().first().show();
Not perfect, but it definitely looks better.
you need to follow the next order, i mean, you need to set on first line the "inputs" and then the "has_many" block, I hope this is what you are looking, regards!
form do |f|
f.inputs "details #1" do
f.input :title, :label => "Title"
f.input :description, :label => "Description"
f.input :image, :as => :file, :required => false, :label=>'Image'
end
f.inputs "details #2" do
f.has_many :subdetails do |a|
a.input :description, :label => "Description"
a.input :image, :as => :file, :required => false, :label=>'Image'
end
end
Ok, after have some problems with the title of has_many I could figure out how is the correct way for do it and i think is more cleaner and fancy...
form do |f|
f.inputs "Post" do
f.input :title
f.input :description
end
f.inputs "Comments" do
f.has_many :comments do |a|
a.input :title
a.input :description
end
end
f.buttons
end
Related
ActiveAdmin automatically pulls up the persisted information for an object when the update form is displayed. It doesn't show the images though - doesn't even show the name in my form - how do I fix this to show the actual image and it's name?
ActiveAdmin.register Art do
permit_params :art_pic_attachments_attributes: [:id, :picture, :_destroy]
form(html: { multipart: true }) do |f|
f.inputs do
f.has_many :art_pic_attachments, allow_destroy: true, heading: 'Images' do |ff|
ff.file_field :picture, required: true, heading: "Pictures"
end
end
f.actions
end
Maybe this wiki article will help. The key is to use an image tag as the input hint.
f.inputs "Attachment", :multipart => true do
f.input :cover_page, :as => :file, :hint => image_tag(f.object.cover_page.url)
f.input :cover_page_cache, :as => :hidden
end
My ActiveAdmin index page for ad_images works fine but new, edit, and view look blank like this...
admin/ad_image.rb looks like this:
ActiveAdmin.register AdImage do
permit_params :name, :url, :imageable_type, :imageable_id
form(:html => { :multipart => true }) do |f|
f.inputs "Ad Image" do
f.input :name
f.input :url, as: :file
f.input :imageable_type
f.input :imageable_id
end
f.actions
end
end
The AdImage model works normally otherwise. Any clue what's going on?
UPDATE
Turns out that "ad_image" conflicts with a formtastic style of the same name... I think? When I remove the "ad_image" class from the form (which is added automatically) everything works fine.
I can give an alias to my ActiveAdmin class like so
ActiveAdmin.register AdImage, as: "ImagesForAds"
... and the form will appear. But now I can't save anything...
Edit admin/ad_image.rb :---
ActiveAdmin.register AdImage do
permit_params :name, :url, :imageable_type, :imageable_id
form do |f|
f.inputs "Ad Image" do
f.input :name
f.input :url, as: :file
f.input :imageable_type
f.input :imageable_id
end
f.actions
end
end
I am facing an issue showing up the error messages in active admin.
I get all the error messages displayed with the fields in the form.
But in the code below, I need atleast one skill and maximum 5 skills to be added.
Else need to throw an error message.
I've added a validation in model as :
validates :skills, :length => { :minimum => 1, :maximum => 5,
:message => " should be atleast 1 and less than 5"}
This validates perfectly, but no error message is displayed.
Can anyone help me with the display of the error message.
Following is the code :
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "User", :multipart => true do
f.input :name
f.input :email, :as => :email
f.input :profile_name
f.input :date_of_birth
f.input :gender, :as => :select, :collection => Gender::GENDERS
end
f.inputs "Skills* ( minimum 1 & maximum 5 )" do
f.has_many :skills do |p|
if !p.object.nil?
# show the destroy checkbox only if it is an existing appointment
# else, there's already dynamic JS to add / remove new appointments
p.input :_destroy, :as => :boolean, :label => "Destroy?",
:hint => "Check this checkbox, if you want to delete this field."
end
p.input :description
p.input :title
end
end
end
end
activeadmin 0.5.1 is available on github.
it contains next line in changelog
"Add support for semantic errors #905 by #robdiciuccio"
here is pull request with this feature
https://github.com/gregbell/active_admin/pull/905
example
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs
f.inputs "Locations" do
f.has_many :locations do |loc|
loc.input :address
loc.input :_destroy, :as => :boolean, :label => "Delete"
end
end
f.buttons
end
to use it add to Gemfile
gem 'activeadmin', :git => "git://github.com/gregbell/active_admin.git", :tag => "v0.5.1"
For passing validation try this
validates_length_of :skills,
:within => 1..5,
:too_short => 'too short message',
:too_long => 'too long message'
I have a infrastructure object composed for many datacenters. In the apps/admin/infrastructures.rb I have the following code:
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters do |datacenter_form|
datacenter_form.input :name
end
end
f.buttons
end
I can add datacenters with no problems but I don't know how I can delete it from infrastructure form.
Sep 2017 Update:
Rails 5.1.4, ActiveAdmin 1.0.0
Append :id and _destroy in permit_params along with other attributes from the model e.g. :name in your case. Then provide the :allow_destroy option in f.has_many too. Other requirements remain the same; like adding allow_destroy: true in accepts_nested_attributes_for.
Final look:
ActiveAdmin.register Infrastructure do
permit_params :name, datacenters_attributes: [:id, :_destroy, :name]
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters, heading: false,
allow_destroy: true,
new_record: false do |datacenter_form|
datacenter_form.input :name
end
end
f.buttons
end
end
ActiveAdmin Reference
This worked for me:
i.input :_destroy, as: :boolean
and in the Model remember to add :allow_destroy :
accepts_nested_attributes_for :images, allow_destroy: true
Solved adding the following line:
datacenter_form.input :_destroy, :as => :boolean, :required => false, :label => 'Remove'
The code looks like:
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters do |datacenter_form|
datacenter_form.input :name
datacenter_form.input :_destroy, :as => :boolean, :required => false, :label => 'Remove'
end
end
f.buttons
end
If you cant destroy the object nested. You need to put :_destroy in your app/admin/object.rb permit_params
permit_params :id,:name, :cod, :_destroy
I hope this will be helpful (I've changed my code to suit your example, so I hope there are no typos here):
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters do |datacenter_form|
datacenter_form.inputs :datacenters do
datacenter_form.input :name
end
datacenter_form.buttons do
link_to "Delete", admin_datacenter_path(datacenter_form.object), method: "delete", class: "button" unless datacenter_form.object.new_record?
end
end
end
f.buttons
end
and the controller method should be defined in datacenters.rb
controller do
def destroy
#datacenter = Datacenter.find(params[:id])
#datacenter.destroy
redirect_to edit_admin_retailer_path(#datacenter.infrastructure)
end
end
This should work:
datacenter_form.label :_delete
datacenter_form.check_box :_delete
This adds a checkbox for each nested object which will delete the object if checked.
Don't forget to add the following to your parent model
has_many :child_name, :dependent => :destroy
I am using ActiveAdmin and Rails 3.1 -- having problem understanding whether the following is a bug, or if there is some way to do it correctly that I am not understanding. I am trying to use a nested model with a has one relationship, so that I can create a page and fill out it's meta data in 1 step. --
(page has_one meta_data, accepts_nested_attributes_for meta_data)
Example 1)
in this example, when I click new page, meta data section is there but there are no input fields -- also, if I edit the record, it shows up correctly, however the fieldset is duplicated in the second section... and if I remove the f.inputs wrapping semantic_field_for (which would make sense), then it breaks completely and shows nothing in the meta data area...
form do |f|
f.inputs "Page Information" do
f.input :name
f.input :uri
f.input :view
f.input :body, :as => :text
f.input :active
end
f.inputs "Meta Data" do
f.semantic_fields_for :meta_data do |meta_form|
meta_form.inputs :title, :description, :keywords, :name => "Meta Information"
end
end
end
I understand the meta data probably isn't being instantiated, but I am not sure how I am supposed to do that in the form block? (or if I can even do it) -- The only way I am able to get this to work is by doing using a custom form, and building the meta data in the view, which looks like this
2) How I am working around it, but seems hacky
<%= semantic_form_for [:admin, #page] do |f| %>
<% #page.build_meta_data %>
<%= f.inputs :name => "Page Information" do %>
<%= f.input :name %>
<%= f.input :uri %>
<%= f.input :view %>
<%= f.input :body, :as => :text %>
<%= f.input :active %>
<% end %>
<%= f.semantic_fields_for :meta_data do |meta_form| %>
<%= meta_form.inputs :title, :description, :keywords, :name => "Meta Information" %>
<% end %>
<%= f.buttons %>
<% end %>
Thanks in advance for any help or clarification.
(note to moderators I started another thread on this but was not as clear and didn't have the workaround solution I do now yet, so if one of the questions should be deleted please delete the other)
I found a better solution for you. You can use :for option in inputs helper.
f.inputs "Meta Data", for: [:meta_data, f.object.meta_data || MetaData.new] do |meta_form|
meta_form.input :title
meta_form.input :description
meta_form.input :keywords
end
I think this might work too, but I didn't check
f.inputs :title, :desctiption, :keywords,
name: "Meta Data",
for: [:meta_data, f.object.meta_data || MetaData.new]
In rails 4, this is something that works, with a nice design
e.g.,
A customer has one account
model/customer.rb
accepts_nested_attributes_for :account
admin/customer.rb
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
i was having the same problem, i worked in your hack and got it working.
i then moved <% #page.build_meta_data %> to a custom new method like this
controller do
def new
#tenant = Tenant.new
#tenant.build_tenant_configurable
end
end
hope this helps