我想使用rails 4作为服务器并将数据发布到rails,然后将数据保存到数据库。为此,我通过http
和update
方法查询数据。但是当服务器和rails中的数据收到想要保存数据时,我会收到Unpermitted parameters: id
错误。我通过以下代码发布数据:
$scope.updateRadif = function(index, ID){
console.log(ID);
RadifSecure.update(index, {bolouk_id: 1, id: ID}).$promise.then(function(data){
//...
})
.catch(function (err) {
//...
})
};
app.factory('RadifSecure', function($resource) {
return $resource("/api/bolouks/:bolouk_id/radifs/:id", { bolouk_id: '@bolouk_id', id: "@id" },
{
'show': { method: 'GET', isArray: false },
'update': { method: 'PUT' },
'destroy': { method: 'DELETE' }
}
);
});
我收到rails服务器中的数据:
def update
@radif = Radif.find(params[:id])
respond_to do |format|
if @radif.update_attributes(radif_params)
format.json { head :no_content }
else
format.json { render json: @radif.errors, status: :unprocessable_entity }
end
end
end
private
def radif_params
params.require(:radif).permit(:bolouk_id, :describe, :price)
end
当我检查服务器日志时,数据被正确接收,但rails不允许保存在数据库中:
Started PUT "/api/bolouks/1/radifs/16?describe=sdsad&price=3423432" for 127.0.0.1 at 2014-09-03 11:14:27 +0430
Processing by Api::V1::RadifsController#update as JSON
Parameters: {"bolouk_id"=>"1", "id"=>"16", "describe"=>"sdsad", "price"=>"3423432", "radif"=>{"id"=>"16", "bolouk_id"=>"1"}}
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 7 ORDER BY "users"."id" ASC LIMIT 1
Radif Load (0.0ms) SELECT "radifs".* FROM "radifs" WHERE "radifs"."id" = ? LIMIT 1 [["id", 16]]
Unpermitted parameters: id
(0.0ms) begin transaction
(0.0ms) commit transaction
Completed 204 No Content in 29ms (ActiveRecord: 0.0ms)
我尝试其他代码来解决这个问题,但我没有成功。
def update
@radif = Radif.find(params[:id])
radif.update!(radif_params)
end
任何人都可以帮我解决这个问题吗?