ruby-on-rails - Grape-文件上传-参数声明

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

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

下面是一个正在进行的类,用于提供个人资料详细信息、更新个人资料详细信息和上传个人资料图片。我得到了这个 params do; end block 来定义必填字段,并且也希望对文件上传执行此操作。但类型会是什么?

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

更新:文件上传本身使用 Carrier-wave 和一个名为 ProfilePictureUploader 的 uploader ,但我怀疑情况是否如此。

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

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

最佳答案

我认为您正在寻找的类型是 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 .

关于ruby-on-rails - Grape-文件上传-参数声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33591110/

相关文章:

ruby-on-rails - 使用 Rails 和 Capistrano 部署在 EC2 上自动扩展

ruby-on-rails - Heroku语法错误;日志包含加扰的行号

carrierwave - 为什么 Carrierwave 和 MiniMagick 图像无法正确合成?

ruby - 在 RACK 服务器启动时运行某些代码

ruby - 为什么 Ruby 服务器仅在守护进程时才会产生僵尸?

swagger - Grape Swagger 描述 JSON 主体

ruby-on-rails - 为什么 simple_form 用所有 comment_titles 填充这些选择列表?

ruby-on-rails - Rails-通过事件资源使用API​​时,URL中具有多个值的参数

ruby-on-rails - rails carrierwave 为 STI 生成的 url

ruby-on-rails - Carrierwave:如何上传大文件?