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