【发布时间】:2014-06-27 19:05:29
【问题描述】:
我有这个:
$scope.create = function() {
var theproperty = {
businessName: this.businessName,
streetAddress: this.streetAddress,
city: this.city,
state: this.state,
zip: this.zip,
mdu: this.mdu,
units: this.units,
content: this.content
};
var propertywrap = {
property: theproperty
}
我将它发送到工厂,工厂将其传递到轨道上。现在,def create 就是这样:
@property = Property.new(params[:property])
然后我有一个 if/else/end 以防我走到那一步。但是,在我真正做到这一点之前,我还没有研究过 safe_params 或任何东西。第一件事,对吧?
get 传递的对象是这样的:
{"property":{"businessName":"test","streetAddress":"test thgis","city":"asdfqw4r","state":"qfa","zip":"asdfas","units":"asdfa","content":"asdfasdf"}}
控制台日志显示:
Started POST "/properties" for 127.0.0.1 at 2014-06-27 13:02:25 -0600
Processing by PropertiesController#create as HTML
Parameters: {"property"=>{"businessName"=>"test from firefox", "streetAddress"=>"12 any st", "city"=>"clearfield", "state"=>"ut", "zip"=>"55445", "units"=>"adf", "content"=>"asdfasdfasdf"}}
Completed 500 Internal Server Error in 1ms
但我收到了回复:ActiveModel::ForbiddenAttributesError
我什至尝试形成我的对象以通过这种方式:
var propertywrap = {
property : {
businessName: this.businessName,
streetAddress: this.streetAddress,
city: this.city,
state: this.state,
zip: this.zip,
mdu: this.mdu,
units: this.units,
content: this.content
}
}
但我仍然得到同样的错误。
ActiveModel::ForbiddenAttributesError at /properties
====================================================
> ActiveModel::ForbiddenAttributesError
我这样做完全疯了吗?我该怎么办?
根据下面的答案,我将参数和表格更改为使用下划线而不是驼峰式。所以,现在我通过了:
{"property":{"business_name":"something her","street_address":"960 s 550 e","city":"sometown","state":"te","zip":"85121","units":"adf","content":"asdfasdf asdfasdf"}}
不过,我仍然遇到同样的错误。
【问题讨论】:
-
这要么是控制器中的强参数,要么是旧版 Rails 模型中不允许的属性
-
我正在使用 rails 4。如何查看强参数是否存在问题?
-
你能从控制器和模型中粘贴创建方法吗?
标签: ruby-on-rails json angularjs ruby-on-rails-4