Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

javascript - AngularJS $resource sending out an extra "registration" hash?

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]": "[email protected]",
    "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]"=>"[email protected]", "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]"=>"[email protected]", "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]: "[email protected]", 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]: "[email protected]"
business[employees_number]: "8"
business[name]: "alice cullen"
business[password]: "123123"
business[postcode]: "3040"
business[suburb]: "essendon"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...