back to list

Rails, AngularJS, and $http

  October 16, 2013 — written by Chris Sloan | 0 comments »

It has definitely been a while since the last post — over a year now. Life's been busy (moved to another state) and so has work, but I've been learning lots of new things to use in my day to day workflow and one of them is AngularJS.

Let me start off by saying Angular is awesome. I never thought I would get into a language that doesn't make me think as I come from the camp of building a Backbone JS app from scratch just so I know what the code is doing. I was even hesitant on utilizing Bootstrap when it first came out to mock up quick layouts in CSS, but have fallen in love with the ease of that too. Maybe it's because I am getting complacent as I get older and want things to be easier, not tougher.

Back to the original reason why I am starting this post. As mentioned, I have been baking in AngularJS to some of the web apps I have been developing lately. There is a ton of documentation out there on it, unlike EmberJS at the time when i was making a decision on which toolkit to use. Don't get me wrong, Ember is probably great, and I love Handlebars, but the flux of the code base pre RC-1 was annoying. On the flip side, Angular documentation, while seemingly more abundant, tends to be conflicting as it too as a code base is still in semi-flux.

I was writing some simple form posting code utilizing Angular JS's $http post methods and noticed in the logs, I was getting a double submission of parameters nested in the controller parameters sent through. This app was developed using Rails. I setup my Angular code just as the docs said for doing a post binding the form fields to specific models so I figured it was compliant and no other magic was happening.

    method: 'POST',
    url: '/myURL',
    data: {entry: $scope.entries}

Which ended up producing results like:

front"=>{"entry"=>{"tourdate_id"=>2, "first_name"=>"Test", "last_name"=>"This", "email"=>"", "phone"=>"123-313-2311", "zip_code"=>"12345"}}}

Come to find out, there is a little bit more that is needed to be added to the headers of the post request and also a parsing of the model params to tell Angular how to send over the params. Modifying the code to now be:

    method: 'POST',
    url: '/myURL',
    data: $.param({entry: $scope.entries}),
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}

Produced the exact results I was looking for:

"entry"=>{"tourdate_id"=>2, "first_name"=>"Test", "last_name"=>"This", "email"=>"", "phone"=>"123-313-2311", "zip_code"=>"12345"}}

It's a wonder why Angular does not have this directly baked in when you do a form post, but at least it was a simple enough change. The fact that I have to utilize jQuery code in it kind of muddies the water as I wanted this to be solely a pure Angular app without that other dependency, but this will do.

filed in: Geekery | permalink

0 shared their opinion. ADD YOURS »

Add your opinion here

Commenting is not available in this weblog entry.
February 2017
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28        
top of page

where you can find me

RailsConf 2010

Flickr Github Working with Rails Linked In

my twitter tweets

follow me