当我在我的Rails 4应用程序上发布ng-resource帖子时,rails没有添加正确的参数,实际上它在帖子中添加了一个名为“base”的意外参数。
我的服务:
App.factory('Rule', ['$resource', function ($resource){
return $resource('/json/rules/:id', {id:'@id'}, {
create: {method: 'POST'},
update: {method: 'PUT'},
delete: {method: 'DELETE'}
});
}]);
Rails路线:
namespace :json, defaults: {format: :json} do
resources :rules
end
Rails控制器:
class Json::RulesController < ApplicationController
respond_to :json
def create
rule = Rule.new(rule_params)
ap rule
rule.save! if rule.valid?
respond_with :json, rule
end
def update
rule = Rule.find(params[:id])
rule.update(rule_params)
respond_with rule
end
def rule_params
params.require(:rule).permit(:business_id, :raw_value => [])
end
end
当我以这种方式在我的应用中发帖时:
$scope.rule.$update(function (){
$location.path('/businesses/{0}'.format($stateParams['id']));
}, SharedMessage.addResponse).finally(function (){
$rootScope.busy = false;
});
http帖子本身发送以下数据:
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate
Accept-Language:en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4
Connection:keep-alive
Content-Length:142
Content-Type:application/json;charset=UTF-8
Cookie:hsfirstvisit=http%3A%2F%2Flocalhost%3A3000%2F%23%2Ftimeline||1402593173474; XSRF-TOKEN=GGNL6suNkGkOUEiUsEPxFE92C0AfCeLsT46GMrAJtoA%3D; __hstc=181257784.ee9ff47c4f98b78ed4d99e2ae1ee5edf.1402593173476.1415037010146.1415207051885.75; __hssrc=1; hubspotutk=ee9ff47c4f98b78ed4d99e2ae1ee5edf; _myapp_session=Uk9LS--914a262148c5784afe30087e8a5f6572fbf7d342
Host:localhost:3000
Origin:http://localhost:3000
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
X-CSRF-TOKEN:GGNL6suNkGkOUEiUsEPxFE92C0AfCeLsT46GMrAJtoA=
X-Requested-With:XMLHttpRequest
X-XSRF-TOKEN:GGNL6suNkGkOUEiUsEPxFE92C0AfCeLsT46GMrAJtoA=
Request Payload:{"raw_value":["from:me"], "business_id":"125", "volume":0}
rails app,接收以下参数:
Parameters: {"raw_value"=>["from:me"], "business_id"=>"125", "volume"=>0, "base"=>{"raw_value"=>["from:me"], "business_id"=>"125", "volume"=>0}}
这里的问题依赖于没有在参数和这个奇怪的“基础”参数中接收对象“规则”。
你们知道点击的可能性吗?
答案 0 :(得分:1)
我不知道angularjs
但让我试着回答。
这里的问题依赖于没有在参数中接收对象'规则'和这个奇怪的'基础'参数
奇怪的base
参数因为你已启用wrap_parameters
设置而存在。检查你的config/initializer/wrap_parameters.rb
。要禁用它,您只需要制作
wrap_parameters false
看着这个
Request Payload:{"raw_value":["from:me"], "business_id":"125", "volume":0}
和
params.require(:rule).permit(:business_id,:raw_value =&gt; [])
这只是意味着你没有以适当的方式发送参数。它应该包含在rule
中,即
Request Payload:{"rule": {"raw_value":["from:me"], "business_id":"125", "volume":0} }
因此,请在angular
的某处进行设置,以便以适当的方式发送params
。