Rails&回形针:编辑页面上传新文件

时间:2014-02-06 22:33:12

标签: ruby-on-rails ruby paperclip

我正在使用Paperclip和Rails 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

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  

1 个答案:

答案 0 :(得分:0)

您可能必须使用连接模型(对此类似has_many :through这样的关联):

<强>模型

#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

<强>控制器

#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

查看

= 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