Found a couple of posts kinda related, but none of them help me with my issue.
发现有几个帖子有点相关,但没有一个能帮我解决问题。
So I created a simple products API using Rails:
所以我使用Rails创建了一个简单的产品API:
class API::V1::ProductsController < ApplicationController
respond_to :json
def index
respond_with Product.all
end
def show
respond_with Product.find(params[:id])
end
def create
@product = Product.new(product_params)
if @product.save
respond_with @product
end
end
def update
respond_with Product.find(params[:id]).update_attributes(params[:product])
end
def destroy
respond_with Product.destroy(params[:id])
end
private
def product_params
params.require(:product).permit(:name, :category, :price, :release_date)
end
end
I'm trying to send a POST request to create a product using RESTClient. when I try to fill in the POST request body with:
我正在尝试发送POST请求以使用RESTClient创建产品。当我尝试用以下内容填写POST请求正文时:
{ "product"=> {
"name": "Acoustic Guitar2",
"category": "Instrument",
"price": 600.0,
"release_date": "2012-01-10"
} }
or
{
"name": "Acoustic Guitar2",
"category": "Instrument",
"price": 600.0,
"release_date": "2012-01-10"
}
or
{ "product": {
"name": "Acoustic Guitar2",
"category": "Instrument",
"price": 600.0,
"release_date": "2012-01-10"
} }
I get an error:
我收到一个错误:
ActionController::ParameterMissing at /api/v1/product
param not found: product
From my research, it has something to do with Rail 4's strong parameters, but I don't really know how to fix it. Any ideas? I know there are ruby-api gems out there, but I'd like to learn how it's done manually.
根据我的研究,它与Rail 4的强大参数有关,但我真的不知道如何修复它。有任何想法吗?我知道那里有ruby-api宝石,但我想了解它是如何手动完成的。
1 个解决方案
#1
3
The real problem is not with the strong parameters. When you write params.require[:product]
, that means that your server will throw a ParameterMissing error if it cannot find the product parameter, and here, it cannot find it ! Why ? Cause as you said, you wrote in the body of the the request, you should pass these arguments as form parameters, I never used RESTClient but there is a difference between form parameters and the body in a request.
真正的问题不在于强大的参数。当你写params.require [:product]时,这意味着如果找不到product参数,你的服务器会抛出一个ParameterMissing错误,在这里,它无法找到它!为什么?正如你所说,你在请求的主体中写道,你应该将这些参数作为表单参数传递,我从不使用RESTClient但是表单参数和请求中的正文之间存在差异。
You should refer to this previous question first : Firefox Add-on RESTclient - How to input POST parameters? Try again, and come back here if it is still not working !
您应首先参考上一个问题:Firefox附加组件RESTclient - 如何输入POST参数?再试一次,如果仍然不能正常工作,请回到这里!
I actually prefer using PostMan (Chrome add on) than REST client, it is easier to use I think.
我实际上更喜欢使用PostMan(Chrome添加)而不是REST客户端,我觉得它更容易使用。
There are several ways of sending post informations.
发送帖子信息有几种方法。
The first request look like this
第一个请求看起来像这样
POST /products HTTP/1.1
Host: 0.0.0.0:3000
Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="product[name]"
foo
----WebKitFormBoundaryE19zNvXGzXaLvS5C
The second one like this
第二个是这样的
POST /products HTTP/1.1
Host: 0.0.0.0:3000
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
product%5Bname%5D=foo
And the third one like this
第三个是这样的
POST /products HTTP/1.1
Host: 0.0.0.0:3000
Cache-Control: no-cache
{'product': {'name': 'foo'}}
the content-type or content-disposition is always different, I think it is the reason why it is changing. You can pass JSON to the Rails server but only if your request is correctly configured ! With Postman, I do not have to think about, it, it works all the time :)
内容类型或内容处置总是不同的,我认为这就是它改变的原因。您可以将JSON传递给Rails服务器,但前提是您的请求已正确配置!有了Postman,我不必考虑,它一直都有效:)
The default content type is probably application/x-www-form-urlencoded. When you are using jQuery, it is the default one at least. For this content type, the second request body has to be provided. However, if you precise a json content-type, it should be fine :)
默认内容类型可能是application / x-www-form-urlencoded。当你使用jQuery时,它至少是默认的。对于此内容类型,必须提供第二个请求正文。但是,如果你精确一个json内容类型,它应该没关系:)
In javascript/jQuery:
$.post('/products', {'product': {'name': 'foo'}}), function(data){/*process response*/}, 'json')
should work properly
应该正常工作
#1
3
The real problem is not with the strong parameters. When you write params.require[:product]
, that means that your server will throw a ParameterMissing error if it cannot find the product parameter, and here, it cannot find it ! Why ? Cause as you said, you wrote in the body of the the request, you should pass these arguments as form parameters, I never used RESTClient but there is a difference between form parameters and the body in a request.
真正的问题不在于强大的参数。当你写params.require [:product]时,这意味着如果找不到product参数,你的服务器会抛出一个ParameterMissing错误,在这里,它无法找到它!为什么?正如你所说,你在请求的主体中写道,你应该将这些参数作为表单参数传递,我从不使用RESTClient但是表单参数和请求中的正文之间存在差异。
You should refer to this previous question first : Firefox Add-on RESTclient - How to input POST parameters? Try again, and come back here if it is still not working !
您应首先参考上一个问题:Firefox附加组件RESTclient - 如何输入POST参数?再试一次,如果仍然不能正常工作,请回到这里!
I actually prefer using PostMan (Chrome add on) than REST client, it is easier to use I think.
我实际上更喜欢使用PostMan(Chrome添加)而不是REST客户端,我觉得它更容易使用。
There are several ways of sending post informations.
发送帖子信息有几种方法。
The first request look like this
第一个请求看起来像这样
POST /products HTTP/1.1
Host: 0.0.0.0:3000
Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="product[name]"
foo
----WebKitFormBoundaryE19zNvXGzXaLvS5C
The second one like this
第二个是这样的
POST /products HTTP/1.1
Host: 0.0.0.0:3000
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
product%5Bname%5D=foo
And the third one like this
第三个是这样的
POST /products HTTP/1.1
Host: 0.0.0.0:3000
Cache-Control: no-cache
{'product': {'name': 'foo'}}
the content-type or content-disposition is always different, I think it is the reason why it is changing. You can pass JSON to the Rails server but only if your request is correctly configured ! With Postman, I do not have to think about, it, it works all the time :)
内容类型或内容处置总是不同的,我认为这就是它改变的原因。您可以将JSON传递给Rails服务器,但前提是您的请求已正确配置!有了Postman,我不必考虑,它一直都有效:)
The default content type is probably application/x-www-form-urlencoded. When you are using jQuery, it is the default one at least. For this content type, the second request body has to be provided. However, if you precise a json content-type, it should be fine :)
默认内容类型可能是application / x-www-form-urlencoded。当你使用jQuery时,它至少是默认的。对于此内容类型,必须提供第二个请求正文。但是,如果你精确一个json内容类型,它应该没关系:)
In javascript/jQuery:
$.post('/products', {'product': {'name': 'foo'}}), function(data){/*process response*/}, 'json')
should work properly
应该正常工作