我想从一些客户端发送POST请求到一个Rails服务器,我有一些问题。完整的要求是发送图像到回形针处理,但它看起来像这是一个普通邮差多部分邮政与Rails问题。邮递员多部分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
我假设的问题是,请求参数没有被封装int“类别”。 请让我知道,如果我不够清楚,如果我可以提供更多的信息。
在此先感谢。
编辑: 正如fylooi建议我已经改变了请求体中加入邮差封装“实体”是这样的:
不过我得到了相同的结果
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'
您好,非常感谢您的回复。我已经尝试在类别[{属性}]中设置邮递员的关键名称,但我仍然得到相同的错误。我将用此修改编辑此问题。谢谢 – Lori
@Lori:在'params.require ...'行之前添加'puts params'并发布日志输出? – fylooi
这是params的外观: {“------ WebKitFormBoundaryaH47lDnnoGYytZoO \ r \ nContent-Disposition:form-data; name”=>“\”category [label] \“\ r \ n \ r \ n流量\ r \ n ------ WebKitFormBoundaryaH47lDnnoGYytZoO \ r \ n内容处置:form-data; name = \“类别[说明] \”\ r \ n \ r \ n交通类别\ r \ n ----- -WebKitFormBoundaryaH47lDnnoGYytZoO - \ r \ n“,”controller“=>”categories“,”action“=>”create“} – Lori