2015-11-08 57 views
2

我正在用葡萄编写我的第一个API,我非常兴奋,它听起来感觉非常好。通过笔记运行我无法找到为文件声明参数的方法。葡萄 - 文件上传 - 参数声明

下面是一个提供个人资料详细信息,更新个人资料详情和上传个人资料图像的未完成课程。我得到这个params do; end块来定义必需的字段,并希望为文件上传做这样的工作。但是这种类型会是什么?

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

更新:文件上传自己使用载波和上传器叫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 

在此先感谢您的支持。那里有美好的一天。

回答

3

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

+0

我需要一副眼镜。有些东西一定是错误的,我的目光不会在文档中看到该部分。谢谢! –