Carrierwave文件上传不起作用 - 保存Nil值

时间:2016-04-11 23:54:32

标签: ruby-on-rails carrierwave image-uploading

我正在尝试使用carrierwave上传/保存数据库中的图像。但我的附件并没有在db上持续存在。只有名称字段存储在表中。

我指的是这个教程:http://www.tutorialspoint.com/ruby-on-rails/rails-file-uploading.htm

经过彻底的研究,我尝试了几种不同的方法来改变强大的障碍。但没有任何效果:(

在桌子上

#<ProjectImage id: 6, name: "Image 3", attachment: nil>

ProjectImagesController:

def new
    @projectImage = ProjectImage.new
  end


  def projectImage_params
      params.require(:project_image).permit(:name, attachment:[])
        #params.require(:project_image).permit(:project_image_attributes=>['name','attachment'])  ---- no luck
        #params.require(:project_image).permit(:name, :attachment,{}) ------ no luck
  end

查看

<%= form_for ProjectImage.new, html: { multipart: true } do |image| %>

     <h4>   <%= image.label :name %>
            <%= image.text_field :name %>
     </h4>
     <h4>
            <%= image.label :attachment %>
             <%= image.file_field :attachment, multiple: true %>
     </h4>
      <%= image.submit "Save" %>
   <% end %>

参数

Parameters: {"utf8"=>"✓", "authenticity_token"=>"I+Q==", "project_image"=>{"name"=>"Image 3", "attachment"=>[#<ActionDispatch::Http::UploadedFile:0x0000000592c4d0 @tempfile=#<Tempfile:C:/Users/xx/AppData/Local/Temp/RackMultipart20160412-7900-1y69rwv.jpg>, @original_filename="jet.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"project_image[attachment][]\"; filename=\"jet.jpg\"\r\nContent-Type: image/jpeg\r\n">]}, "commit"=>"Save"}

    [#<ActionDispatch::Http::UploadedFile:0x0000000592c4d0 @tempfile=#<Tempfile:C:/Users/STUART~1.MIN/AppData/Local/Temp/RackMultipart20160412-7900-1y69rwv.jpg>, @original_filename="jet.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"project_image[attachment][]\"; filename=\"jet.jpg\"\r\nContent-Type: image/jpeg\r\n">]



class ProjectImage < ActiveRecord::Base 
    mount_uploader :attachment, AttachmentUploader 
end

AttachmentUploader类

class AttachmentUploader < CarrierWave::Uploader::Base
storage :file
  uploader = AttachmentUploader.new
def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

    def extension_white_list
      %w(jpg jpeg gif png)
   end
 end

任何帮助都会受到赞赏! 干杯

1 个答案:

答案 0 :(得分:0)

从&lt; '//Convert Dec to Hex strData(0) = intPatternColorsRed(1).ToString("x") 删除multiple:true使这项工作成功 干杯