How to create an association between two models with form choice field using Active Admin, Paperclip in Rails 5.0.1

trickydiddy

Scenario

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:

enter image description here

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:

enter image description here

enter image description here

enter image description here

Questions:

  1. How can I have a preview of the image in the admin edit view?

  2. How can build a form to associate an image to a room by choosing the listing_name of the room?

Something like this:

enter image description here

Thank you in advance for your help.

trickydiddy

Solution:

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails: How to create an object that belongs to two models

From Dev

Interacting with 5 models in a rails form

From Dev

How to create voting to a multiple choice application using ruby on rails

From Dev

Remove paperclip images Active Admin

From Dev

"wrong number of arguments (1 for 0)" error loading file input field with Rails 4, Formtastic and Paperclip

From Dev

Cannot create a model with 'binary' field using Active Admin and Rails 4.1

From Dev

Ruby on Rails Project using association models

From Dev

Using Amazon AWS SDK instead of Paperclip for active admin

From Dev

How to create a complex file field using rails form helpers?

From Dev

How to render custom html form using active admin

From Dev

Rails - one form to two models

From Dev

Ruby on Rails, User Models and Active Admin query

From Dev

How to create choice field in django model using another model

From Dev

Cannot create a record with association Active Admin

From Dev

How to includes associated models in active_admin

From Dev

Rails Active Admin Resource Association Belongs To

From Dev

Cannot create a model with 'binary' field using Active Admin and Rails 4.1

From Dev

Ruby on Rails Project using association models

From Dev

association between two models

From Dev

How to set the same value for two inputs for two models in a Rails form

From Dev

Association between two models not working

From Dev

Association problems in between models RAILS 4

From Dev

Ruby on Rails Paperclip Association Dropdown

From Dev

Rails 4 - Association between two models

From Dev

Active admin multiple image upload with paperclip Rails 5

From Dev

How do I update a postgres boolean field using Active Record in Rails 5?

From Dev

Associate Rails active model with two different models

From Dev

Uploading more than one image using paperclip and active admin

From Dev

Create Association Through Two Different Models

Related Related

  1. 1

    Rails: How to create an object that belongs to two models

  2. 2

    Interacting with 5 models in a rails form

  3. 3

    How to create voting to a multiple choice application using ruby on rails

  4. 4

    Remove paperclip images Active Admin

  5. 5

    "wrong number of arguments (1 for 0)" error loading file input field with Rails 4, Formtastic and Paperclip

  6. 6

    Cannot create a model with 'binary' field using Active Admin and Rails 4.1

  7. 7

    Ruby on Rails Project using association models

  8. 8

    Using Amazon AWS SDK instead of Paperclip for active admin

  9. 9

    How to create a complex file field using rails form helpers?

  10. 10

    How to render custom html form using active admin

  11. 11

    Rails - one form to two models

  12. 12

    Ruby on Rails, User Models and Active Admin query

  13. 13

    How to create choice field in django model using another model

  14. 14

    Cannot create a record with association Active Admin

  15. 15

    How to includes associated models in active_admin

  16. 16

    Rails Active Admin Resource Association Belongs To

  17. 17

    Cannot create a model with 'binary' field using Active Admin and Rails 4.1

  18. 18

    Ruby on Rails Project using association models

  19. 19

    association between two models

  20. 20

    How to set the same value for two inputs for two models in a Rails form

  21. 21

    Association between two models not working

  22. 22

    Association problems in between models RAILS 4

  23. 23

    Ruby on Rails Paperclip Association Dropdown

  24. 24

    Rails 4 - Association between two models

  25. 25

    Active admin multiple image upload with paperclip Rails 5

  26. 26

    How do I update a postgres boolean field using Active Record in Rails 5?

  27. 27

    Associate Rails active model with two different models

  28. 28

    Uploading more than one image using paperclip and active admin

  29. 29

    Create Association Through Two Different Models

HotTag

Archive