Reputation: 671
I have a very simply angular form that looks like this:
<form ng-submit="createNewQuestion()" name="createNewQuestionForm">
<div class="form-group">
<textarea class="form-control" rows="3" ng-model="question.question" placeholder="Add a question here."></textarea>
</div>
<div class="form-group">
<textarea class="form-control" rows="3" ng-model="question.answer" placeholder="Add the answer here."></textarea>
</div>
<button type="submit" class="btn btn-default">Next</button>
</form>
It submits to the CreateNewQuestion() function in the controller, which looks like this:
$scope.createNewQuestion = function () {
$scope.question.droplet_id = $scope.createdDroplet.id; //add droplet_id to form data
question.create($scope.question).success(function(question) {
$scope.questions.push(question); //return the question and put it in the helps variable
$scope.question = {}; // clear form
});
};
This calls the factory for the POST request which looks like this:
app.factory('question', ['$http', function($http) {
return {
create: function (question) {
return $http.post('/questions/', question)
}
}
}]);
The request gets made on submit but the params are in the wrong format. The log looks like this:
Processing by QuestionsController#create as HTML
Parameters: {"question"=>"some_question", "answer"=>"some_answer", "droplet_id"=>785}
Completed 500 Internal Server Error in 3ms
NoMethodError (undefined method `permit' for "vhjkhfjhgfjhg":String):
Here's the weird thing. I have identical code with copy and pasted forms and functions that sends the data in the correct format for my rails backend, which would be:
Parameters: {"question"=>"question", "answer"=>"answer", "drop_id"=>485, "question"=>{"question"=>"question", "answer"=>"answer", "drop_id"=>485}}
The fields are different but the code is literally identical. Why is one working while the other is not and how can I fix it?
Update
It seems to be a naming issue on the backend, so the request is okay. If I remove require(:question) from:
params.require(:question).permit(:question, :answer, :droplet_id, :ordering)
it works. It seems it doesn't like the model name being the same as one of the attributes.
Upvotes: 0
Views: 68
Reputation: 671
The problem was not with the request but with the way my rails backend handled the request. Because the model name was also the name of one of the columns it became confused and would not transform the request into the right format to get it through the strong params.
The solution was to change the column name. After that, everything worked fine.
Upvotes: 1