我正在构建Rails API。并使用邮递员测试API。当通过邮递员上传文件时,我不会在后端得到文件数组。尽管我得到了ActionDispatch::Http::UploadedFile类型。正因为如此,我才不会在post_params中获得我的post_params。如何修复它。
这里是我的控制器类:
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发布于 2018-06-13 05:23:28
尽管我得到了
ActionDispatch::Http::UploadedFile类型
在postman中,您需要将字段名更改为avatars[],然后可以将化身作为数组:
{"avatars"=>[#ActionDispatch::Http::UploadedFile....]}您可以按照avatars[]发送带/不带索引的this answer参数。
https://stackoverflow.com/questions/50829098
复制相似问题