0

New to AngularJS here and have some trouble getting $resource to work. I've created a factory and hardcoded the $resource object with what my backend needs in order to create a new plumber.

However, when I call the function, instead of passing only the params that I've entered, it creates a 'sort of' duplicate content in the form of a registration hash, as shown in hte backend of my app (bolded below). It's basically an exact duplicate of my params. Where did it come from???

The create() function is called by a button in the plumbers/new.html template

<button data-ng-click="create()">create</button>

Where did that hash come from???

new.js

angular.module('ngappApp')
.controller('PlumbersNewCtrl', function ($scope, $window, $resource, Plumbers) {

$scope.create = function(){
  var test1 = new Plumbers({
    "business[email]": "superhero@super.com",
    "business[password]": "123123",
    "business[name]": "alice cullen",
    "business[company]": "alice pty ltd",
    "business[abn]": "12312312",
    "business[contact_number]": "0421772800",
    "business[address]": "118 glass street",
    "business[suburb]": "essendon",
    "business[postcode]": "3040",
    "business[employees_number]": "8"
  });

  test1.$save();
};

});

angular.module('ngappApp')
.factory('Plumbers', function($resource){
return $resource('/businesses');
});

And the response from the backend:

Started POST "/businesses" for 127.0.0.1 at 2013-08-26 20:40:27 +1000 Processing by Businesses::RegistrationsController#create as JSON Parameters: {"business[email]"=>"superhero@super.com", "business[password]"=>"[FILTERED]", "business[name]"=>"alice cullen", "business[company]"=>"alice pty ltd", "business[abn]"=>"12312312", "business[contact_number]"=>"0421772800", "business[address]"=>"118 glass street", "business[suburb]"=>"essendon", "business[postcode]"=>"3040", "business[employees_number]"=>"8", "registration"=>{"business[email]"=>"superhero@super.com", "business[password]"=>"[FILTERED]", "business[name]"=>"alice cullen", "business[company]"=>"alice pty ltd", "business[abn]"=>"12312312", "business[contact_number]"=>"0421772800", "business[address]"=>"118 glass street", "business[suburb]"=>"essendon", "business[postcode]"=>"3040", "business[employees_number]"=>"8"}} WARNING: Can't verify CSRF token authenticity (0.1ms) begin transaction (0.0ms) rollback transaction Completed 400 Bad Request in 6ms (Views: 0.1ms | ActiveRecord: 0.2ms)

EDIT:

Logging test1 shows the correct params without the 'extra'

Resource {business[email]: "superhero@super.com", business[password]: "123123",                                           business[name]: "alice cullen", business[company]: "alice pty ltd",
business[abn]: "12312312"
business[address]: "118 glass street"
business[company]: "alice pty ltd"
business[contact_number]: "0421772800"
business[email]: "superhero@super.com"
business[employees_number]: "8"
business[name]: "alice cullen"
business[password]: "123123"
business[postcode]: "3040"
business[suburb]: "essendon"
Ryan W Kan
  • 380
  • 3
  • 16

1 Answers1

1

It's not doubled. Rails is wrapping your params inside of the registrations parameter, but you see both, cause it still lets that through. I'm also writing an angularjs app on Rails.

BTW, why are you wrapping your code like that? If you want to wrap it inside of a hash then you need to do:

business = {name: "Name", blah:"Blah"}

Allen
  • 794
  • 1
  • 6
  • 19
  • so how do I stop that? I've tried sending the same params with cURL and rails accepted it. Now that I am sending with AngularJS, it's rejecting, what should I be doing to make it accept? – Ryan W Kan Aug 26 '13 at 11:12
  • The guy who is exposing the back end told me that the accepted key, value pair is business[key]:"value". – Ryan W Kan Aug 26 '13 at 11:20
  • If I do business = {name: "Name", blah:"Blah"}, I get Parameters: {"business"=>{"name"=>"Alice C", "password"=>"[FILTERED]", "email"=>"alicec@plumbing.com"} ... etc... Did I implement your way like you intended? – Ryan W Kan Aug 26 '13 at 11:22
  • Turns out it didn't matter. It can be turned on or off though, from config/initializers/wrap_parameters.rb – Ryan W Kan Aug 26 '13 at 12:03
  • Yes, exactly. I knew that. Sorry, I went home after work. I don't know if that way, what you showed above, works or not, but it seems off cause it should be a hash of hashes with values, but if it works, then it shouldn't be an issue. Is everything working finally? What's not passing through? What type of Rails version is it? Are you using strong_parameters? Are you allowing all of the correct values in? – Allen Aug 26 '13 at 14:51
  • you're right it didn't matter. When I tested the API with postman chrome extension, I used businesses[key] and it worked so I thought that was it. After your tip from your first reply, I tested it and found out that it was actually converted into a business hash of hashes. So bottom line is that I was passing in wrong values. Once I changed it into the format you proposed everything works! Rails 3.2, so no strong_parameters – Ryan W Kan Aug 28 '13 at 00:46