2015-08-27 20 views
3

我想从一些客户端发送POST请求到一个Rails服务器,我有一些问题。完整的要求是发送图像到回形针处理,但它看起来像这是一个普通邮差多部分邮政与Rails问题。邮递员多部分POST到Rails

这就是我得到: enter image description here 娄我的设置:

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 

enter image description here

enter image description here

我假设的问题是,请求参数没有被封装int“类别”。 请让我知道,如果我不够清楚,如果我可以提供更多的信息。

在此先感谢。

编辑: 正如fylooi建议我已经改变了请求体中加入邮差封装“实体”是这样的: enter image description here

不过我得到了相同的结果

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' 

回答

5

邮差工作正常与Rails,你只需要了解Rails通常如何处理参数。

比方说,您发布以下参数到服务器:

plain_param=value 
nested_object[attribute]=value 

这被解析为以下:

pry(main)> params = ActionController::Parameters.new(plain_param:"value", nested_object: { attribute: "value" }) 
=> {"plain_param"=>"value", "nested_object"=>{"attribute"=>"value"}} 

让我们来看看如何permit作品。

params.permit(:plain_param) 
pry(main)> params.permit(:plain_param) 
Unpermitted parameter: nested_object 
=> {"plain_param"=>"value"} 

pry(main)> params.permit(:nested_object) 
Unpermitted parameters: plain_param, nested_object 
=> {} 

pry(main)> params.permit(:nested_object => :attribute) 
Unpermitted parameter: plain_param 
=> {"nested_object"=>{"attribute"=>"value"}} 

pry(main)> params.permit(:plain_param, :nested_object => :attribute) 
=> {"plain_param"=>"value", "nested_object"=>{"attribute"=>"value"}} 

到目前为止,这么好。看起来像permit返回顶层的整个哈希值并嵌套允许的密钥,并打印未授权密钥的警报。 require怎么样?

[33] pry(main)> params 
=> {"plain_param"=>"value", "nested_object"=>{"attribute"=>"value"}} 

pry(main)> params.require(:plain_param) 
=> "value" 

pry(main)> params.require(:nested_object) 
=> {"attribute"=>"value"} 

pry(main)> params.require(:nested_object => :attribute) 
ActionController::ParameterMissing: param is missing or the value is empty: {:nested_object=>:attribute} 

pry(main)> params.require(:plain_param, :nested_object) 
ArgumentError: wrong number of arguments (2 for 1) 

我们可以看到require返回单个参数密钥的值。这有助于确保具有多个属性的对象的存在。

结束语:

params.require(:category).permit(:label, :description) 

期望形式

{:category=>{:label=>"value", :description=>"value"}}

其转换为

category[label]=value 
category[description]=value 

编辑的HTML POST参数的散列:邮差自动地设置content-type多部分文件头文件头oad,所以不要手动设置。不确定这是否被视为错误或功能。

https://github.com/postmanlabs/postman-app-support/issues/191

+0

您好,非常感谢您的回复。我已经尝试在类别[{属性}]中设置邮递员的关键名称,但我仍然得到相同的错误。我将用此修改编辑此问题。谢谢 – Lori

+0

@Lori:在'params.require ...'行之前添加'puts params'并发布日志输出? – fylooi

+0

这是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