【问题标题】:ActionDispatch::Http::UploadedFile to array for Active Storage Rails 5.2ActionDispatch::Http::UploadedFile 到 Active Storage Rails 5.2 的数组
【发布时间】:2018-11-22 13:20:08
【问题描述】:

我正在构建 Rails API。并使用 Postman 测试 API。通过 Postman 上传文件时,我没有在后端获取文件数组。尽管我得到了ActionDispatch::Http::UploadedFile 的类型。因此,我没有在post_params 中获得我的avatar。如何解决。

这是我的控制器类:

class Api::V1::PostsController < Api::V1::BaseController
  skip_before_action :authenticate, only: [:create]
  before_action :current_timeline, only: [:create]

  def create
    post = current_timeline.posts.build(post_params)
    binding.pry
    if post.save
      render_success(:created, post, meta: {message: 'Post sucessfully added'})
    else
      render_error(421, post.errors)
    end

  end

  private

  def post_params
    params.require(:post).permit(:message, :user_id, :occasion_id, :timeline_id, avatars: [])
  end

  def current_timeline
    Timeline.find(post_params[:timeline_id])
  end
end

【问题讨论】:

    标签: ruby-on-rails postman


    【解决方案1】:

    尽管我得到了ActionDispatch::Http::UploadedFile的类型

    在postman中,需要将字段名改为avatars[],然后就可以通过数组获取头像了:

    {"avatars"=>[#ActionDispatch::Http::UploadedFile....]}
    

    您可以根据this answer 发送avatars[] 参数,括号中带/不带索引

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-01
      相关资源
      最近更新 更多