【问题标题】:Grape - File Upload - Parameter declaration葡萄——文件上传——参数声明
【发布时间】:2015-11-08 05:41:03
【问题描述】:

我正在使用 Grape 编写我的第一个 API,我非常兴奋,它听起来和感觉都很棒。浏览笔记我找不到为文件声明参数的方法。

下面是一个正在进行中的类,用于提供个人资料详细信息、更新个人资料详细信息和上传个人资料图片。我得到了这个params do; end 块来定义必填字段,并且也想为文件上传这样做。但是会是什么类型呢?

试图在网上找到一个例子,我遇到的少数没有使用它。可能是一个微不足道和愚蠢的问题,但我发现很难找到它。

更新:它自己上传的文件使用 Carrier-wave 和一个名为 ProfilePictureUploader 的上传器,但我怀疑是这种情况。

class AccountApi < Grape::API

  resource :account do

    desc 'View the current user profile'
    get :profile do
      present current_user, with: Presenters::UserPresenter
    end

    desc 'Update the current user profile'
    params do
      requires :email,      type: String,   desc: 'User email'
      requires :first_name, type: String,   desc: 'First name'
      requires :last_name,  type: String,   desc: 'Last name'
      requires :phone,      type: String,   desc: 'Phone number'
      requires :school_id,  type: Integer,  desc: 'School ID'
    end
    put :profile do
    end

    desc 'Upload profile picture'
    # params do
    #   requires :user, type: Hash do
    #     requires :profile_picture, type: <<??????>>, desc: 'User profile picture'
    #   end
    # end
    post :profile_picture do
      profile_picture = params[:user][:profile_picture]

      status = current_user.update(profile_picture: profile_picture)

      {
        status: status,
        size: profile_picture[:tempfile].size,
      }
    end

  end

end

提前感谢您的支持。你在那里度过了愉快的一天。

【问题讨论】:

    标签: ruby-on-rails carrierwave grape grape-api


    【解决方案1】:

    我认为您要查找的类型是 Rack::Multipart::UploadedFile 或只是 File

    params do
       requires :user, type: Hash do
         requires :profile_picture, type: Rack::Multipart::UploadedFile, desc: 'User profile picture'
       end
     end
    

    这是grape 支持的一种类型,如here

    【讨论】:

    • 我需要一副眼镜。我的视力一定有问题,无法在文档中看到该部分。谢谢!
    猜你喜欢
    • 2015-06-13
    • 1970-01-01
    • 2017-05-01
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    相关资源
    最近更新 更多