ruby on rails - $http service adding unwanted JSON key to request data -
i have simple service i'm using post data rails controller.
my service looks this:
app.service('autorulesservice', function($http) { return({ createrule: createrule }); function createrule() { var request = $http({ method: 'post', url: '/rules.json', data: { one: 'two } }); return request.then(handlesuccess, handleerror); } function handlesuccess() { // body omitted... } function handleerror() { // body omitted... } });
i use service in controller in pretty standard way:
$scope.saverule = function() { rulesservice.createrule().then(function() { // stuff... }); }
the problem weird unwanted key in parameters when inspect sent data in rails log. "rule"
parameter coming from?
processing autoanalysis::rulescontroller#create json parameters: {"one"=>"two", "rule"=>{}}
it doesn't appear in request payload (as inspected in chrome dev tools)
and controller action pretty standard (there's no before filters either):
class rulescontroller < applicationcontroller def create # note: i'm referencing :auto_analysis_rule parameter here because # that's desired param key name. doesn't exist in request # shown here. render json: rule.create(params[:auto_analysis_rule]) end end
and can't find mention of $http
inferring root json key url or in docs.
where "rule"
param key coming from?
rails automatically wraps parameters attributes of model. if one
attribute of rule model, payload like: {"rule" => {"one" => "two"}}
this functionality removes need top-level key contains attributes. in other words, following payloads treated same if attr1
, attr2
fields in mymodel
model:
{ "mymodel" : { "attr1" : "val1", "attr2" : "val2" } } { "attr1" : "val1", "attr2" : "val2" }
this functionality can disabled per-controller or app-wide in initializer. check out answer more information: rails 3 params unwanted wrapping
Comments
Post a Comment