【问题标题】:How can I pass a nested json object to rails from angular如何将嵌套的 json 对象从角度传递给 rails
【发布时间】: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


【解决方案1】:

问题的原因可能是商业名称和街道地址等参数的驼峰式。在 Rails 端,这个属性应该是 business_name 和 street_address。

你可以使用 https://github.com/FineLinePrototyping/angularjs-rails-resource “默认情况下,我们在下划线和驼峰式之间转换属性名称。”

【讨论】:

  • 好的,我将发布的参数更改为使用下划线,我还更新了表格以匹配它。所以,现在我通过了:{"property":{"business_name":"something her","street_address":"960 s 550 e","city":"sometown","state":"te", "zip":"85121","units":"adf","content":"asdfasdf asdfasdf"}} 仍然出现同样的错误。
  • 也许你忘记了控制器中的强参数,检查这个stackoverflow.com/a/17335871/905697 你应该有这样的东西:``` @property = Property.new(property_params) def property_params params.require(:property ).permit(:business_name, :street_address) # 在此处添加 rest end ```
  • 我必须拥有那个吗?我以为这是可选的?在我的 ruby​​ 控制器中查看,我可能让它寻找胡椒参数,而我没有使用强参数。
  • Rails 4 应用默认启用强参数。在将参数传递给 Propery 构造函数之前,您需要允许它们。检查github.com/rails/strong_parameters
  • 你猜怎么着?就是这样。我没有意识到我必须设置强大的参数。我认为这是可选的。一旦我这样做了,它就完美了。我将我的操作更改为 new 以创建,为参数设置一个私有方法,我很高兴。你可以在这里看到我更新的控制器:github.com/jshultz/rails-omniauth/blob/master/app/controllers/…
猜你喜欢
  • 2013-10-19
  • 2019-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-22
  • 1970-01-01
  • 2011-08-30
相关资源
最近更新 更多