I have a crud with paperclip and multiple images' i implement active_admin and the product update fine, but, i can't not upload or edit the multiple images, the form i have is this:
form :html => { :multipart => true } do |f|
f.inputs "Details" do
f.input :name
f.input :created_at, :label => "Publish Product at"
f.input :category
end
f.inputs "Images" do
f.has_many :assets do |p|
p.input :asset, :as => :file, :input_html => { :multiple => true }, :label => "Image", :hint => p.object.asset.nil? ? p.template.content_tag(:span, "No Image Yet") : p.template.image_tag(p.object.asset.url(:thumb))
p.input :_destroy, :as=>:boolean, :required => false, :label=>'Remove'
end
end
f.inputs "Content" do
f.input :description
end
f.buttons
end
and the...
f.inputs "Images" do
f.has_many :assets do |p|
p.input :asset, :as => :file, :input_html => { :multiple => true }, :label => "Image", :hint => p.object.asset.nil? ? p.template.content_tag(:span, "No Image Yet") : p.template.image_tag(p.object.asset.url(:thumb))
p.input :_destroy, :as=>:boolean, :required => false, :label=>'Remove'
end
end
I want to upload images, but when i create a new asset this have a missing image default and not attach the correct image, i think because the path from the images is not correct to upload. My asset model is:
class Asset < ActiveRecord::Base
belongs_to :product
has_attached_file :asset, :styles => { :large => "340x330", :medium => "140x80>", :thumb => "70x70>" },
:url => "/products/:id/:style/:basename.:extension",
:path => ":rails_root/public/products/:id/:style/:basename.:extension"
end
how i can modify my assets form to work like i want? Thank you!
The Solution
Hi, here is the solution, the key are how work the nested attributes in formtastic...
form :html => { :multipart => true } do |f|
f.inputs "Product information" do
f.input :name
f.input :description
end
f.inputs "Product images" do
f.has_many :assets do |p|
p.input :asset, :as => :file, :label => "Image",:hint => p.object.asset.nil? ? p.template.content_tag(:span, "No Image Yet") : p.template.image_tag(p.object.asset.url(:thumb))
p.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove image'
end
end
f.inputs "Product details" do
f.input :category, :label => "Category", :hint => "Select one category"
f.input :height
f.input :width
f.input :depth
f.input :color, :label => "Color", :hint => "Select one color"
f.input :sku, :label => "SKU"
f.input :price
end
f.buttons
end
Related
I am new to ruby on rails and active admin, I need to create check boxes for registration fees. while I checked, the value from checkbox is not saved to the database. can anyone help me ??
form do |f|
f.inputs do
f.input :name, label: "Student Name"
f.input :dob,:label => "Date of Birth"
f.input :age,:label => "Age"
f.input :gender, as: :radio, :label => "Gender", :collection => [ "Male", "Female"]
f.input :reg_fee, :as => :check_boxes, :collection => RegChargeSetup.all.map{|v| ["#{v.reg_fee}"]}
end
f.actions
end
From your code, you are taking array of arrays, instead you should take array of strings,
So, change
f.input :reg_fee, :as => :check_boxes, :collection => RegChargeSetup.all.map{|v| ["#{v.reg_fee}"]}
to,
f.input :reg_fee, :as => :check_boxes, :collection => RegChargeSetup.all.map{|v| "#{v.reg_fee}"}
form do |f|
f.inputs do
f.input :name, label: "Student Name"
f.input :dob,:label => "Date of Birth"
f.input :age,:label => "Age"
f.input :gender, as: :radio, :label => "Gender", :collection => [ "Male", "Female"]
f.input :reg_fee, :as => :check_boxes, :collection => RegChargeSetup.all.map{|v| "#{v.reg_fee}"}
end
f.actions
end
I have created a form in activeadmin as follows. When I select the bkid of the customer, the fields customer name, mobile number, total amount, and amount paid have to be filled automatically in the form.
form :class => 'form_inline' do |f|
f.inputs do
f.input :bkid, :label => 'Booking Id', :as => :select, :collection => Package.all.map{|u| ["#{u.bkid}"]}
f.input :firstName, :as => :string, :label => 'First name', :class => 'same'
f.input :lastName, :as => :string, :label => 'Last name', :class => 'same'
f.input :mobile, :as => :string, :label => 'Mobile no', :class => 'same'
f.input :totalAmount, :as => :string, :label => 'Total Amount', :class => 'same'
f.input :amountPaid, :as => :string, :label => 'Amount Paid', :class => 'same'
f.input :currentPayment, :as => :string, :label => 'Current Payment', :class => 'same'
# f.input :accountcode_id, :label => 'Acc_code', :as => :select, :collection => Accountcode.all.map{|u| ["#{u.accountcode}, #{u.accountname}"]}
br
f.submit :value => 'submit'
end
end
How can I achieve this using ajax?
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"
In a form in active admin, the fields expand horizontally across the page according to the size of the window. If it's a big monitor, there is a lot of "unused" space to the right.
How can I add a "column" (NOT a side bar) to the right of the page so that I will end up with a 50% width section of the form on the left and a 50% width section of the form on the right?
I need this because I have lots of fields.
This is what my form partially looks like right now...
form do |f|
f.inputs "Shipment Details" do
f.input :file_number
f.input :customer, :label_method => :company_name
f.input :shipper, :label_method => :company_name
f.input :broker, :label_method => :company_name
end
f.inputs "Places" do
f.input :place_of_origin, :as => :select, :collection => Place.find(:all, :order => "city", :select => "city").map(&:city)
f.input :place_of_loading, :as => :select, :collection => Place.find(:all, :order => "city", :select => "city").map(&:city)
f.input :place_of_delivery, :as => :select, :collection => Place.find(:all, :order => "city", :select => "city").map(&:city)
f.input :via, :as => :select, :collection => Place.find(:all, :order => "city", :select => "city").map(&:city)
end
f.inputs "Carrier" do
f.input :carrier, :label_method => :company_name
f.input :mode, :as => :select,
:collection => ["Air", "Air Collect", "Air Prepaid", "FCL", "FTL", "LCL", "LTL", "TBA"]
f.input :mbl, :label => "MBL"
f.input :hbl, :label => "HQL"
f.input :vessel
f.input :container
end
f.buttons
end
It's pretty simple, you can give each section of your inputs a CSS class, then you would modify active_admin.css.scss (or just .css) so it would float your forms to the correct place, as well as give them a correct width, etc.
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