【问题标题】:Rails & Paperclip: Edit page upload a new fileRails & Paperclip:编辑页面上传新文件
【发布时间】:2014-02-07 19:05:15
【问题描述】:

我正在使用带有 Rails 4 的 Paperclip 来上传图像。目标是允许用户在编辑页面上添加新图像。目前,用户能够查看与模型关联的当前图像并删除它们,但我希望用户能够在同一页面上上传新图像。

由于表单已提交到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   

上传.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  

【问题讨论】:

  • 你能分享你的车吗,上传模型关联。我假设 Upload 模型具有 has_attached_file 关联。
  • 已在问题中更新

标签: ruby-on-rails ruby paperclip


【解决方案1】:

您可能必须使用连接模型(为此使用 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 

【讨论】:

  • 这是我的起点,但需要调整
猜你喜欢
  • 2013-09-04
  • 2014-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-22
相关资源
最近更新 更多