【问题标题】:Rails api not accepting video uploadsRails api不接受视频上传
【发布时间】:2020-05-21 17:26:30
【问题描述】:

我有一个 rails api,我正在尝试设置一个控制器,您可以在其中上传视频。您上传此视频的方式是使用表单数据。我尝试了不同的方法来保存我的视频,但没有任何效果。这可能是参数的问题,但我不是 100% 确定。当我运行 if @video.save 时,它总是返回 false。在发布请求中,我发送了一个 ID(用于用户 ID)、一个描述,并在表单数据中发送了一个 mov 视频(我已将验证设置为视频/快速时间)。这是我要将信息发送到的控制器:

uploads_controller.rb

class UploadsController < ApplicationController
  respond_to :json
  before_action :authenticate_user!, only: [:create]
  protect_from_forgery with: :null_session

  def create
    @user = User.find(params[:id])
    @video = Video.new(video_params)
    if @user == current_user
      if @video.save
        render :create 
      else
        render json: {status: 'Video could not save'}, status: :not_found
    else
      render json: {status: 'Invalid token'}, status: :not_found
    end
  end

  private

  def video_params
    params.permit(:clip, :description, :id)
  end
end

我也在使用 activestorage 将这些视频上传到 s3。来自控制台的错误(发送 post 请求后)是 Rendered ActiveModel::Serializer::Null with Has

【问题讨论】:

  • 你为什么不从模型status: @video.errors.full_messages.join(', ')输出错误,这样你就可以调试问题了
  • 没有给我太多。在 Rails 控制台中,它只是说 completed 0,而在邮递员中,状态显示为 CUSTOM。
  • 好的,问题可能出在video_params 方法中,如果您使用form_for 构建表单,您可能需要以这种方式允许参数params.require(:video).permit(:clip, :description, :id) ...一个猜测,也许您可​​以编辑您的问题并添加表单以查看参数的外观

标签: ruby-on-rails


【解决方案1】:

我想我明白了。正确的控制器参数是:params.require(:video).permit(:clip, :description)。您还应该更改发布请求,因为旧请求不起作用,所以它看起来像这样:

{
    "id": 1,
    "video": {
        "description": "Diss"
    }
}

然后您将在表单数据中插入视频。但是,您不能在 Postman 中轻松完成此操作,因此您必须找到另一种发送方式。

编辑:没有办法测试这个很容易完成的解决方案,所以我不能 100% 确定。

【讨论】:

    猜你喜欢
    • 2017-09-21
    • 2014-02-24
    • 2016-01-12
    • 2013-01-23
    • 2015-06-10
    • 2017-09-03
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多