레일 클립 4가있는 클립을 사용하여 이미지를 업로드하고 있습니다. 목표는 편집 페이지에서 사용자가 새 이미지를 추가 할 수있게하는 것입니다. 현재 사용자는 모델과 관련된 현재 이미지를보고 삭제할 수 있지만 사용자가이 동일한 페이지에서 새 이미지를 업로드 할 수 있기를 바랍니다.레일 및 페이퍼 클립 : 페이지 편집 새 파일 업로드
양식이 controller#update
에 제출되었으므로 모델의 인스턴스 변수와 연결된 새 이미지를 작성하는 방법을 모르겠습니다.
= 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 당신은 아마 (이것에 대한 has_many :through
같은 협회) 조인 모델을 사용해야합니다
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
당신은 당신의 자동차, 업로드 모델 연결을 공유 할 수 있습니다. 업로드 모델에 has_attached_file 연관이 있다고 가정합니다. – blotto
질문이 – Patrick