angularjs - Angular POST request not formatting correctly -
i have angular form looks this:
<form ng-submit="createnewquestion()" name="createnewquestionform"> <div class="form-group"> <textarea class="form-control" rows="3" ng-model="question.question" placeholder="add question here."></textarea> </div> <div class="form-group"> <textarea class="form-control" rows="3" ng-model="question.answer" placeholder="add answer here."></textarea> </div> <button type="submit" class="btn btn-default">next</button> </form>
it submits createnewquestion() function in controller, looks this:
$scope.createnewquestion = function () { $scope.question.droplet_id = $scope.createddroplet.id; //add droplet_id form data question.create($scope.question).success(function(question) { $scope.questions.push(question); //return question , put in helps variable $scope.question = {}; // clear form }); };
this calls factory post request looks this:
app.factory('question', ['$http', function($http) { return { create: function (question) { return $http.post('/questions/', question) } } }]);
the request gets made on submit params in wrong format. log looks this:
processing questionscontroller#create html parameters: {"question"=>"some_question", "answer"=>"some_answer", "droplet_id"=>785} completed 500 internal server error in 3ms nomethoderror (undefined method `permit' "vhjkhfjhgfjhg":string):
here's weird thing. have identical code copy , pasted forms , functions sends data in correct format rails backend, be:
parameters: {"question"=>"question", "answer"=>"answer", "drop_id"=>485, "question"=>{"question"=>"question", "answer"=>"answer", "drop_id"=>485}}
the fields different code literally identical. why 1 working while other not , how can fix it?
update
it seems naming issue on backend, request okay. if remove require(:question) from:
params.require(:question).permit(:question, :answer, :droplet_id, :ordering)
it works. seems doesn't model name being same 1 of attributes.
the problem not request way rails backend handled request. because model name name of 1 of columns became confused , not transform request right format through strong params.
the solution change column name. after that, worked fine.
Comments
Post a Comment