1

I'm using Paperclip with Rails 4 to upload images. The goal is to allow users to add new images while on the edit page. Currently a user is able to view current images associated to the model and delete them, but I would like a user to be able to do upload a new image on this same page.

Since the form is submitted to the controller#update I'm not sure how to create a new image associated to the instance variable of the model.

= form_for @car, html: { multipart: true, id: "fileupload", "data-abide" => true } do |f|    
...
  %h4                                                                                                                                                                     
    New files                                                                                                                                                             
    = f.fields_for :uploads do |upload_fields|                                                                                                                            
      = upload_fields.file_field :upload                                                                                                                                  
  - unless f.object.new_record?                                                                                                                                           
    %h4                                                                                                                                                                   
      Old Files                                                                                                                                                           
    = f.fields_for :uploads do |upload_fields|                                                                                                                            
      .thumb                                                                                                                                                              
        = link_to image_tag(upload_fields.object.upload.url(:thumb)), upload_fields.object.upload.url(:original)                                                          
        = upload_fields.check_box :_destroy 

Car.rb

class Car < ActiveRecord::Base                                                                                                                                                
  has_many :uploads, dependent: :destroy                                                                                                                                      


  validates :make, :model, :year, :seats, :transmission, :drive, :interior, :exterior, presence: true                                                                         

  accepts_nested_attributes_for :uploads, allow_destroy:  true                                                                                                                

  # http://stackoverflow.com/questions/2672744/rails-activerecord-find-all-users-except-current-user/2674308#2674308                                                           
  scope :without_car, lambda{|car| car ? {conditions: ["id != ?", car.id]} : {} }                                                                                             

end   

Upload.rb

class Upload < ActiveRecord::Base                                                                                                                                             
  belongs_to :car                                                                                                                                                             

  has_attached_file :upload, styles: { medium: '300x300>', thumb: '100x100>' }                                                                                                

  validates_attachment_content_type :upload, :content_type => /\Aimage\/.*\Z/                                                                                                 
  validates_attachment_presence :upload                                                                                                                                       

end  
Patrick
  • 1,410
  • 2
  • 19
  • 37

1 Answers1

0

You'll probably have to use a join model (with an association like has_many :through for this):

Models

#app/models/car.rb
Class Car < ActiveRecord::Base
    has_many :car_uploads
    has_many :uploads, through: :car_uploads

    accepts_nested_attributes_for :car_uploads
end

#app/models/car_upload.rb
Class CarUpload < ActiveRecord::Base
    belongs_to :car
    belongs_to :upload

    accepts_nested_attributes_for :upload
end

#app/models/upload.rb
Class Upload < ActiveRecord::Base
    has_many :car_uploads
    has_many :uploads, through: :car_uploads
end

Controllers

#app/controllers/cars_controller.rb
def update
   @car = Car.find(params[:id])
   @car.update(car_params) #-> not sure if that will work
end

private

def car_params
    params.require(:car).permit(car_uploads_attributes: [upload_attributes:[:upload]])
end

View

= form_for @car, html: { multipart: true, id: "fileupload", "data-abide" => true } do |f|    
...
  %h4                                                                                                                                                                     
    New files                                                                                                                                                             
    = f.fields_for :car_uploads do |car_upload_fields| 
        = car_upload_fields.fields_for :uploads do |u|                                                                                                                              
          = upload_fields.file_field :upload                                                                                                                                  
  - unless f.object.new_record?                                                                                                                                           
    %h4                                                                                                                                                                   
      Old Files                                                                                                                                                           
    = f.fields_for :car_uploads do |car_upload_fields| 
       = car_upload_fields.fields_for :uploads do |u|                                                                                                                         
          .thumb                                                                                                                                                              
            = link_to image_tag(u.object.upload.url(:thumb)), u.object.upload.url(:original)                                                          
            = u.check_box :_destroy 
Richard Peck
  • 76,116
  • 9
  • 93
  • 147