JSON 请求的强参数
Strong parameters with JSON request
我认为如果请求被识别为 JSON,Rails 会自动 identifies/parses JSON 参数。但是下面的请求:
Processing by Api::V1::LinksController#create as JSON
Parameters: {"link"=>"{\"title\":\"My first title\"}"}
以及以下参数方法:
def link_params
params.require(:link).permit(:title)
end
导致此错误:
NoMethodError (undefined method `permit' for "{\"title\":\"My first title\"}":String):
任何想法这里的约定是什么以获得强大的参数 + json 工作将不胜感激。
更新
这是发出请求的代码(使用 http 客户端 axios):
axios({
method: 'post',
url: '/api/v1/links.json',
responseType: 'json',
params: {
link: {
title: "My first title"
}
},
})
.then( (response) => {
});
根据文档 here
axios({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
});
将params:
替换为data:
。
我认为如果请求被识别为 JSON,Rails 会自动 identifies/parses JSON 参数。但是下面的请求:
Processing by Api::V1::LinksController#create as JSON
Parameters: {"link"=>"{\"title\":\"My first title\"}"}
以及以下参数方法:
def link_params
params.require(:link).permit(:title)
end
导致此错误:
NoMethodError (undefined method `permit' for "{\"title\":\"My first title\"}":String):
任何想法这里的约定是什么以获得强大的参数 + json 工作将不胜感激。
更新
这是发出请求的代码(使用 http 客户端 axios):
axios({
method: 'post',
url: '/api/v1/links.json',
responseType: 'json',
params: {
link: {
title: "My first title"
}
},
})
.then( (response) => {
});
根据文档 here
axios({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
});
将params:
替换为data:
。