I have a rails app which works as an api, here my controller:
class CarController < ApplicationController
def create
@car = Car.new(params[:car])
if @car.save
render json: @car, status: :created, location: @car
else
render json: @car.errors, status: :unprocessable_entity
end
end
end
I want to "call" this create function from my angular app, here my angular factory
angular_app.factory('api', function($http) {
$http.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content');
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
return {
//car looks like {owner:'Andres', year:'2014', ....} with every field of Car model
SaveCar: function(car) {
return $http.post('/car/', {car:car}).then(function(result) {
return result;
})
}
}
});
The problem is that in my create car function I get someting like this:
{"{\"car\":{\"owner\":\"andres\",\"year\":\"2014\",....}}"=>nil, "action"=>"create", "controller"=>"car"}
just an string, and it should be:
{"car" => {"owner"=>"andres", "year":"2014", ....}, "action"=>"create", "controller"=>"task"}
What is my mistake here? I tried to use JSON.parse
but didn't work.
params[:car]
useparams.require(:car).permit!
. But it throw another error in this case. All seems ok. Except there is no routes for this request. – zishe May 27 '14 at 23:33ActionController::ParameterMissing (param is missing or the value is empty: car):
– Andres May 27 '14 at 23:36car
in angular part is a string, not object. log it. – zishe May 27 '14 at 23:39var car = {owner:"Andres", year:"2014", ....}
– Andres May 27 '14 at 23:41