【问题标题】:How to set all attributes to UploadedFile in Rails 4如何在 Rails 4 中将所有属性设置为 UploadedFile
【发布时间】:2016-02-25 20:22:55
【问题描述】:

我有一个 API,我从 Base64 的 Angular 客户端发送了一个图像,我的方法创建的下一个代码:

if params[:profile][:picture]
        picture_params = params[:profile][:picture]


        #Create new Temporal File 
        temp_file = Tempfile.new("file_to_upload")
        temp_file.binmode
        temp_file.write(Base64.decode64(picture_params["body"]))

        #Create a new uploaded file
               uploaded_file = ActionDispatch::Http::UploadedFile.new(
                :tempfile => temp_file, 
                :original_filename => picture_params[:name]) 

        #After replace picture with the new uploaded file
        params[:profile][:picture] = uploaded_file
        Rails.logger.info "#{params[:profile]}"
    end

    @profile = @current_user.build_profile(profile_params)
    if @profile.save
        render "api/v1/profiles/show"
    else
        errors_array!(@profile.errors.full_messages, :unprocessable_entity)
    end

当我打印参数后,信息为零

"picture"=>#<ActionDispatch::Http::UploadedFile:0x007f872daac5b0 @tempfile=#<Tempfile:/var/folders/nx/30024wp17ggfgw542lyzs4zw0000gn/T/file_to_upload20160225-14952-by947r>, @original_filename=nil, @content_type=nil, @headers=nil>}

在尝试创建配置文件后,我遇到了下一个错误,我认为这是针对上一个问题

NoMethodError (undefined method `gsub' for nil:NilClass):
 app/controllers/api/v1/profiles_controller.rb:39:in `create'

我在 Rails 4.2.5.1 中使用回形针 gem 上传文件

【问题讨论】:

    标签: ruby-on-rails angularjs ruby ruby-on-rails-4 paperclip


    【解决方案1】:

    感谢收看,不过有解决办法,我改了方法:

        def set_selfies(selfie_params)
          picture_params = selfie_params
          encoded_picture = picture_params[:body]
          content_type = picture_params[:content_type]
          image = Paperclip.io_adapters.for("#{encoded_picture}")
          image.original_filename = picture_params[:name]
          image
        end
    

    I Read this Blog: 就是这样,对我有用,谢谢:D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-27
      • 2017-07-12
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      相关资源
      最近更新 更多