I have a simple angular service that returns a $resource
. Lets say it looks like this:
我有一个返回$resource的简单的角度服务。假设它是这样的:
$resource '/users/:id/:options.json', {id: '@id', options: '@options'},
create_user: {
method: 'POST'
}
Now in my controller I use this resource by creating a new instance of my user service:
现在,在我的控制器中,我通过创建用户服务的一个新实例来使用这个资源:
user = new UserService()
user.name = $scope.user.name
user.email = $scope.user.email
user.password = $scope.user.password
user.password_confirmation = $scope.user.password_confirmation
user.$create_user()
But when the last line is executed, the data is sent but it is not in a object.
但是,当执行最后一行时,发送数据,但数据不在对象中。
Now I have Devise managing my user account and looking into their registration controller, it seems they expect me to send the data in an object called: sign_up
.
现在我设计了管理我的用户帐户并查看他们的注册控制器,他们似乎希望我将数据发送到一个名为:sign_up的对象中。
How can I describe in the $resource
, that I want my data to be encapsulated in my custom object?
我如何在$resource中描述,我希望我的数据被封装在我的自定义对象中?
EDIT: This is what server gets. It is not happy with it and says that every value is missing.
编辑:这是服务器得到的。它对它不满意,说所有的价值都不见了。
Parameters: {"name"=>"foo", "email"=>"foo@bar.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "registration"=>{"name"=>"foo", "email"=>"foo@bar.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
EDIT2: Working server log with .erb
registration:
使用。erb注册的工作服务器日志:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xMEDtMWf72yuTwbkaq21c0ZKynoBSWnSypXfm+xJYvNph1d4iKqLIBwwe0DSA3I/YCU+pAHDd23ylc4y7ximEQ==", "user"=>{"name"=>"lol", "email"=>"lol@lol.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
2 个解决方案
#1
1
I would recommend using transformRequest on that route. Your code would look something like this.
我建议在这条路上使用transformRequest。你的代码应该是这样的。
$resource('/users/:id/:options.json',
{id: '@id', options: '@options'},
{
create_user: {
method: 'POST',
transformRequest: function(data, headers) {
return angular.toJson({
user: data
});
}
}
});
This allows you to change the data being sent. The data object passed to transformRequest is your User object. The headers will already have the Content-Type application/json so you don't need to add that here.
这允许您更改发送的数据。传递给transformRequest的数据对象是用户对象。header中已经有Content-Type应用程序/json,所以不需要在这里添加。
#2
1
This should work:
这应该工作:
var User = $resource(
'/users/:id/:options.json', {id: '@id', options: '@options'},
create_user: {
method: 'POST'
}
);
User.create_user({user: $scope.user});
#1
1
I would recommend using transformRequest on that route. Your code would look something like this.
我建议在这条路上使用transformRequest。你的代码应该是这样的。
$resource('/users/:id/:options.json',
{id: '@id', options: '@options'},
{
create_user: {
method: 'POST',
transformRequest: function(data, headers) {
return angular.toJson({
user: data
});
}
}
});
This allows you to change the data being sent. The data object passed to transformRequest is your User object. The headers will already have the Content-Type application/json so you don't need to add that here.
这允许您更改发送的数据。传递给transformRequest的数据对象是用户对象。header中已经有Content-Type应用程序/json,所以不需要在这里添加。
#2
1
This should work:
这应该工作:
var User = $resource(
'/users/:id/:options.json', {id: '@id', options: '@options'},
create_user: {
method: 'POST'
}
);
User.create_user({user: $scope.user});