【发布时间】:2015-11-21 13:06:14
【问题描述】:
我正在尝试从某个客户端向 Rails 服务器发送 POST 请求,但遇到了一些问题。完整的要求是发送要由回形针处理的图像,但它看起来像是普通的邮递员多部分POST 有 Rails 问题。
class CategoriesController < ApplicationController
def create
@category = Category.new(category_params)
respond_to do |format|
if @category.save
format.html { redirect_to @category, notice: 'Category was successfully created.' }
format.json { render :show, status: :created, location: @category }
else
format.html { render :new }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
private
def category_params
params.require(:category).permit(:label, :description)
end
我假设问题是请求参数未封装在“类别”中。 如果我不够清楚,请告诉我,如果我能提供更多信息。
提前致谢。
编辑: 正如 fylooi 所建议的,我已经更改了 Postman 中的请求正文,添加了一个封装的“实体”,如下所示:
我仍然得到相同的结果
Processing by CategoriesController#create as JSON
Parameters: {"------WebKitFormBoundaryFdJXZFMuAl0fZf3Q\r\nContent-Disposition: form-data; name"=>"\"category[label]\"\r\n\r\nTraffic\r\n------WebKitFormBoundaryFdJXZFMuAl0fZf3Q\r\nContent-Disposition: form-data; name=\"category[description]\"\r\n\r\nTraffic category\r\n------WebKitFormBoundaryFdJXZFMuAl0fZf3Q--\r\n"}
Completed 400 Bad Request in 1ms (ActiveRecord: 0.0ms)
ActionController::ParameterMissing (param is missing or the value is empty: category):
app/controllers/categories_controller.rb:67:in `category_params'
app/controllers/categories_controller.rb:27:in `create'
【问题讨论】:
标签: ruby-on-rails multipartform-data postman