I am building a similar App like AirBnB but the difference is that only the Admin can add new Apartments, Rooms, Houses etc. Therefore I created an Admin Panel with the activeadmin gem. I am now adding the image upload system into activeadmin so I am using the paperclip gem. I have two models "room" and "photo". The photo model has an "image" column. The room model has an "listing_name" column.
room.rb (app/models)
class Room < ApplicationRecord
has_many :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
If I am not putting a custom form inside "photo.rb (app/admin)" I can associate the image to a room by default. But I can't upload a picture! This looks like this:
So I changed the "photo.rb (app/admin)" file into this:
photo.rb (app/admin)
ActiveAdmin.register Photo do
permit_params :image
form :html => {:multipart => true} do |f|
f.inputs "Project Details" do
f.input :image, :required => false, :as => :file
end
f.actions
end
show do |ad|
attributes_table do
row :image do
image_tag(ad.image.url(:thumb))
end
end
end
end
With this form I can upload a picture into the image column but I can't associate it to a room and the edit view does not give me a preview of a picture as the show view does:
How can I have a preview of the image in the admin edit view?
How can build a form to associate an image to a room by choosing the listing_name of the room?
Something like this:
Thank you in advance for your help.
photo.rb
...
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
row :room_id do
photo.room ? photo.room.listing_name : ""
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_id, Room.all,:id,:listing_name
end
f.actions
end
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments