0

I am building Rails API. And using Postman to test the API. While uploading file through Postman I am not getting array of files in backend. In spite I am getting the type ActionDispatch::Http::UploadedFile. Because of that I am not getting my avatar in post_params. How to fix it.

Here is my controller class:

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
Sourabh Banka
  • 1,080
  • 3
  • 24
  • 48

1 Answers1

0

In spite I am getting the type ActionDispatch::Http::UploadedFile

In postman, you need to change the field name to avatars[], then you can get avatars as array:

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

You can send avatars[] param with/without index in brackets as per this answer

Moamen Naanou
  • 1,683
  • 1
  • 21
  • 45