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
Related
I think a relatively easy question I've had a tough time with grasping in Rails.
How do I get Rails to accept multiple values for an object's property? And how do I display those items. Currently, when I try to display, it has blank fields after I create the item.
I'm using Active Admin so the permits looks a bit different below but same concept as would be in a controller.
Here's code (the input form and params are below):
ActiveAdmin.register Object do
# See permitted parameters documentation:
# https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
#
permit_params :name, :description, :price, :style_number, :design, :picture, :materials, :country_made_in, collection_colour_id: []
#
# or
#
# permit_params do
# permitted = [:permitted, :attributes]
# permitted << :other if params[:action] == 'create' && current_user.admin?
# permitted
# end
form do |f|
f.semantic_errors *f.object.errors.keys
f.input :name
f.input :style_number, required: true
f.input :design, required: true
f.input :materials, required: true
f.input :country_made_in, required: true
f.input :description
f.input :collection_colour_id, multiple: true, required: true, :label => 'Colours', :as => :check_boxes, :collection => CollectionColour.all.collect {|u| [u.colour_name, u.id]}
f.input :collection_colour_id, multiple: true, required: true, :label => 'Colours', :as => :check_boxes, :collection => CollectionColour.all.collect {|u| [u.colour_name, u.id]}
f.inputs "Upload" do
f.input :picture, required: true, as: :file
end
f.input :price
f.actions
end
end
The collection_colour_id collects from a collection_colour model. Currently when I create a new object, doesn't accept any values for collection_colour. Appreciate any help!
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 wan't to add image upload field inside active admin interface.This is the view where I want to be able get photo upload
I tried some earlier suggestions From here
ActiveAdmin.register Product do
form :html => { :multipart=>true } do |f|
f.inputs :new_product do
f.input :name
f.input :price
f.input :category
f.input :description
f.has_many :prod_images do |p|
p.input :photo, :as => :file, :label => "Image",:hint => p.template.image_tag(p.object.photo.url(:thumb))
p.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove image'
end
end
f.buttons
end
Using this example I got such error
undefined method `klass' for nil:NilClass
It says that error is from here app/views/active_admin/resource/new.html.arb where line #1 raised
but how can I access that file, because in explorer it is not showing up?
Thanks
Try building a prod image, like this.
f.has_many :prod_images, f.object.prod_images.build do |p|
I managed to get file upload field with this code
ActiveAdmin.register Product do
form :html => { :enctype => "multipart/form-data" } do |f|
f.input :photo, :as => :file, :hint => f.template.image_tag(f.object.photo.url(:thumb))
end
But now I can't add submit buttons :D So I am still working on it :)
EDIT
ActiveAdmin.register Product do
form :html => { :enctype => "multipart/form-data" } do |f|
f.input :photo, :as => :file
f.buttons
end
end
This just shows buttons like Create and Cancel, but not showing up file field, I checked formastic examples, but without success.
EDIT2
class Product < ActiveRecord::Base
attr_accessible :category_id, :description, :manufacturer_id, :name, :photo
extend FriendlyId
has_attached_file :photo,
:styles => {
:thumb=> "100x100#",
:large => "290x170",
:medium=> "120x120"}
friendly_id :name, use: [:slugged, :history]
belongs_to :manufacturer
belongs_to :category
end
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
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