Active admin multiple image upload with paperclip Rails 5 - ruby-on-rails

I use Active admin and I need upload photos for my project.
How can I do it? My code:
class Project < ApplicationRecord
has_many :images
accepts_nested_attributes_for :images
end
class Image < ApplicationRecord
belongs_to :project
has_attached_file :image
validates_attachment_presence :image
end
ActiveAdmin.register Project do
permit_params :project_name , :project_location , :project_status , :project_area , :project_prices , :project_info , :project_description , :image , :image_file_name, :image_content_type, :image_file_size, :image_updated_at
index do
column :project_name
column :project_description
actions
end
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs 'Project Info' do
f.input :project_name
f.input :project_description
end
f.inputs do
f.has_many :images do |p|
p.input :image , as: :file
end
end
f.actions
end
end
With this code i can create Project without image. But i can't add any image to db.
Waiting for help !!!

I solve the problem ! working code :
permit_params :project_name , :project_location , :project_status , :project_area , :project_prices , :project_info , :project_description , images_attributes: [:image , :id , :_destroy]
f.inputs do
f.has_many :images , heading: false, allow_destroy: true do |ff|
ff.input :image, required: true, as: :file
end
end
most important part that i miss is : images_attributes: [:image , :id , :_destroy] if you don't write this part fully , it won't work !

Related

has_one relation in form in active admin

I have two models/tabels: room and room_location, that have a one on one relation:
class Room < ApplicationRecord
has_one :room_location
end
class RoomLocation < ApplicationRecord
belongs_to :room
end
And this is what i want to do in my form in rooms.rb:
ActiveAdmin.register Room do
menu parent: 'Rooms'
permit_params :name, :description, room_location_attributes: [:address, :city]
form do |f|
f.inputs 'Roomsdata' do
f.input :name, as: :string
f.input :description
f.has_one :room_location do |t|
t.inputs do
t.address
t.city
end
end
f.actions
end
end
end
The has_one doesnt work and if i do has_many, it says relation "room_locations" does not exist
You should write in the params room_location_id instead of attributes
ActiveAdmin.register Room do
menu parent: 'Rooms'
permit_params :name, :description, room_location_id
form do |f|
address_id = ''
cs = params[:id].present? ? Case.find(params[:id]) : nil
unless cs.nil?
address_id = cs.address.id unless cs.address.nil?
end
f.inputs 'Roomsdata' do
f.input :name, as: :string
f.input :description
f.input :room_location_id , :as => :select, :collection => room_location.order(address: :desc).to_a.uniq(&:address).collect {|room_location| [room_location.address, room_location.id] }, selected: room_location_id
f.input :room_location_id , :as => :select, :collection => room_location.order(city: :desc).to_a.uniq(&:city).collect {|room_location| [room_location.address, room_location.id] }, selected: room_location_id
f.actions
end
end
end

ActiveAdmin: Not saving association between my two models

How can the association between my two models be saved in ActiveAdmin?
I have two models a room and a photo model. In the ActiveAdmin I want to update the association between the photo and the room.
room.rb (app/models)
class Room < ApplicationRecord
has_many :photos
accepts_nested_attributes_for :photos
end
photo.rb (app/models)
class Photo < ApplicationRecord
belongs_to :room
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
photo.rb (app/admin)
ActiveAdmin.register Photo do
permit_params :image, , rooms_attributes: [:id, :listing_name, :room_id, :photo_id, :images]
index do
selectable_column
id_column
column :image_file_name
column :listing_name
column :room
column :updated_at
column :created_at
actions
end
show do |image|
attributes_table do
row :image do
photo.image? ? image_tag(photo.image.url, height: '100') : content_tag(:span, "No photo yet")
end
end
active_admin_comments
end
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs do
f.input :image, hint: f.photo.image? ? image_tag(f.photo.image.url, height: '100') : content_tag(:span, "Upload JPG/PNG/GIF image")
end
f.inputs do
f.collection_select :room, Room.all,:id,:listing_name
end
f.actions
end
The form seems to work but it does not save it to the database when I check the record(last room) in the rails console it always returns me room_id: nil? I have tried everything nothing seems to work. Please help.
UPDATE
I have added, "rooms_attributes: [:id, :listing_name, :room_id, :photo_id, :images]" to the params in the photo.rb(admin). I have also have added :photo_id to the room.rb(admin) file.
But it still does not work! Any hints welcomed! If someone needs further information just let me know.
Try
permit_params :image, room_id
f.collection_select :room_id, Room.all, :id, :listing_name

ActiveAdmin form: multiple nested forms

I have a structure User-> Profile-> Image and want to use one form for editing and recording in ActiveAdmin.
I use accepts_nested_attributes_forin models:
class User < ActiveRecord::Base
has_one :profile, dependent: :destroy;
accepts_nested_attributes_for :profile, allow_destroy: true
end
class Profile < ActiveRecord::Base
belongs_to :image, dependent: :destroy;
accepts_nested_attributes_for :image,:reject_if => proc { |attributes| !attributes['img'].present? }, :allow_destroy => true
end
class Image < ActiveRecord::Base
has_attached_file :img
validates_attachment_content_type :img, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end
And such permit_params in ActiveAdmin.register User:
permit_params do
permitted=[:id,:login, :email, :admin, :password, :password_confirmation, :ip_address];
permitted.append(profile_attributes:[:name,:second_name,:middle_name,:img,:mobile_phone,:country, :city,:region, image_attributes:[:img]]);
permitted
end
Finally, the code itself forms
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs "User Details" do
f.input :login
f.input :email
f.input :password
f.input :password_confirmation
end
f.inputs "Profile", for: [:profile, f.object.profile || f.object.build_profile] do |pf|
pf.input :name
pf.input :second_name
pf.input :middle_name
pf.input :mobile_phone, :as => :phone
pf.input :country, selected: "RU"
pf.input :city
pf.input :region
pf.inputs "Avatar", for:[:image, pf.object.image || pf.object.build_image] do |fpf|
fpf.input :img, :as => :file
end
end
f.inputs "User Perference" do
f.input :admin, type: :boolean
end
f.actions
end
Unfortunately, this code does not work: the form is correctly displayed with Profile and work, but the form is not visible to the Image. How can I fix this?
Unfortunately, I have not found the right solution for this problem, and it is still relevant (I'm not going to close this question, just in case someday it will answer).
But I modeled the behavior close to the desired one.
So, to the User model I added Avatar-interface for the respective object.
def avatar
if self.profile && self.profile.image
self.profile.image.img
else
nil
end
end
def avatar=(arg)
unless self.profile.image
self.profile.create_image(img:arg)
else
self.profile.image.update_attributes(img:arg)
end
self.profile.image
end
def avatar?
if self.profile && self.profile.image
!self.profile.image.img.nil?
else
nil
end
end
In ActiveAdmin added to the avatar permit_params for User.
permit_params ..., :avatar
Finally, in the form of added file fields:
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs "User Details" do
#Fields for User
end
f.inputs "profile", for: [:profile, f.object.profile || f.object.build_profile] do |pf|
#Fields for profile
end
f.inputs "Avatar" do
f.input :avatar, as: :file
end
f.actions
end
It is worth noting that if you have observed abnormal behavior when the form is submitted, with empty values, then you should pay attention to the parameters for accepts_nested_attributes_for, in particular update_only and reject_if.
I'm still waiting for the normal solution for multiple nested forms in ActiveAdmin

Nested form in activeadmin not saving updates

I have a nested form in ActiveAdmin for these models (a :class_section has_many :class_dates):
class ClassDate < ActiveRecord::Base
belongs_to :class_section
validates :start_time, :presence => true
validates :end_time, :presence => true
end
and
class ClassSection < ActiveRecord::Base
belongs_to :class_course
has_many :class_dates
belongs_to :location
accepts_nested_attributes_for :class_dates
end
Everything seems to be in the right place when I look at the form. However, when I update a class_date, it doesn't save.
ActiveAdmin.register ClassSection do
permit_params :max_students, :min_students, :info, :class_course_id, :location_id
form do |f|
f.inputs "Details" do
f.input :class_course, member_label: :id_num
f.input :min_students, label: "Minunum Students"
f.input :max_students, label: "Maxiumum Students"
f.input :location
end
f.inputs do
f.input :info, label: "Additional Information"
end
f.inputs "Dates" do
f.has_many :class_dates, heading: false do |cd|
cd.input :start_time, :as => :datetime_picker
cd.input :end_time, :as => :datetime_picker
end
end
f.actions
end
index do
column :class_course
column :location
default_actions
end
filter :class_course
filter :location
show do |cs|
attributes_table do
row :class_course do
cs.class_course.id_num + " - " + cs.class_course.name
end
row :location
row :min_students, label: "Minunum Students"
row :max_students, label: "Maxiumum Students"
row :info, label: "Additional Information"
end
panel "Dates" do
attributes_table_for class_section.class_dates do
rows :start_time, :end_time
end
end
active_admin_comments
end
end
Here is the ActiveAdmin file for ClassDates:
ActiveAdmin.register ClassDate, as: "Dates" do
permit_params :start_time, :end_time, :class_section_id
belongs_to :class_section
end
Can you see a reason why it's not saving properly?
UPDATE: I added the following code to the AA and it seems to work now:
controller do
def permitted_params
params.permit!
end
end
Let me know if there is a better solution. Thanks.
UPDATE 2: There is one lingering problem however. I am unable to delete ClassDates using this form.
You need to permit nested parameters, but you should never use params.permit!. It's extremely unsafe. Try this:
ActiveAdmin.register ClassSection do
permit_params :max_students, :min_students, :info, :class_course_id, :location_id,
class_dates_attributes: [ :id, :start_time, :end_time, :_destroy ]
form do |f|
# ...
f.inputs "Dates" do
f.has_many :class_dates, heading: false, allow_destroy: true do |cd|
cd.input :start_time, :as => :datetime_picker
cd.input :end_time, :as => :datetime_picker
end
end
f.actions
end
# ...
end
The configuration (and permitted_params) of your ClassDate admin panel has nothing to do with the permitted parameters within the ClassSection admin panel. Treat them as separate controllers within the app.
Adding the allow_destroy: true option to the has_many call will add a checkbox to the form to allow you to delete a class time upon form submission.

active_admin and adding multiple images to gallery

I'm using active_admin and carrierwave gems. Have two simple models:
class Image < ActiveRecord::Base
attr_accessible :gallery_id, :file
belongs_to :gallery
mount_uploader :file, FileUploader
end
class Gallery < ActiveRecord::Base
attr_accessible :description, :title, :file, :images_attributes
has_many :images
accepts_nested_attributes_for :images, allow_destroy: true
mount_uploader :file, FileUploader
end
Now my active_admin form for Gallery looks like this:
form do |f|
f.inputs "Gallery" do
f.input :title
end
f.has_many :images do |ff|
ff.input :file
end
f.actions
end
Now I can upload one file, click "Add New Image" and upload another one. Instead of it, I'd like to click "Add new Image", select multiple files and upload them all at once. Any idea how can I implement it?
For a Gallery form with multiple image uploads you can try this
admin/galleries.rb
form do |f|
f.inputs "Gallery" do
f.input :name
end
f.has_many :images do |ff|
ff.input :file
end
end
In model/gallery.rb:
attr_accessible :images_attributes
In model/gallery.rb (add after relations):
accepts_nested_attributes_for :images, :allow_destroy => true

Resources