gpt4 book ai didi

javascript - 主干和 Rails 关联 : Avoiding JSON HashWithIndifferentAccess errors

转载 作者:搜寻专家 更新时间:2023-11-01 04:44:11 24 4
gpt4 key购买 nike

我试图让我的主干关联在 Rails 应用程序中工作,但我在尝试更新现有模型时遇到了困难。具体来说,Rails 会抛出以下错误:

Started PUT "/posts/2" for 127.0.0.1 at 2012-01-04 02:36:14 +1000
Processing by PostsController#update as JSON Parameters: {"post"=>{"content"=>"Seconderona", "created_at"=>"2012-01-03T10:51:09Z", "id"=>2, "title"=>"Second test post", "updated_at"=>"2012-01-03T10:51:09Z", "comments"=>[{}]}, "id"=>"2"} Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "2"]] WARNING: Can't mass-assign protected attributes: id Completed 500 Internal Server Error in 15ms

ActiveRecord::AssociationTypeMismatch (Comment(#70104367824560) expected, got ActiveSupport::HashWithIndifferentAccess(#70104367278120)):
app/controllers/posts_controller.rb:62:in block in update'<br/>
app/controllers/posts_controller.rb:61:in
update'

一些事情:

这是触发于(例如):

c = window.router.comments.models[0]
c.save({content: 'Changed content'})

此外,是的,模型中存在“accepts_nested_attributes_for”。

下面的(有问题的)代码几乎是从 thoughtbot 的“rails Backbone ”电子书中逐字提取的,我也尝试过遵循 Backbone 关系 gem 的文档。两者都会引发此错误。任何想法表示赞赏,下面的代码

RAILS“发布”模型

class Post < ActiveRecord::Base
has_many :comments

accepts_nested_attributes_for :comments

def as_json(options = nil)
super((options || {}).merge(include: { comments: { only: [content] } } ))
end
end

RAILS“评论”模型

class Comment < ActiveRecord::Base
belongs_to :post

accepts_nested_attributes_for :post

def as_json(options = nil)
super((options || {}).merge(include: { post: { only: [:title, :content]}}))
end
end

Backbone 后 Controller

class Backbonerelationaldemo.Models.Post extends Backbone.Model
paramRoot: 'post'

initialize: () ->
comments = new Backbonerelationaldemo.Collections.CommentsCollection
comments.reset(@get('comments'))
@setComments(comments)

setComments: (comments) ->
@comments = comments


class Backbonerelationaldemo.Collections.PostsCollection extends Backbone.Collection
model: Backbonerelationaldemo.Models.Post
url: '/posts'

Backbone 评论 Controller

class Backbonerelationaldemo.Models.Comment extends Backbone.Model
paramRoot: 'comment'

initialize: () ->
if (@has('post'))
@setPost(new Backbonerelationaldemo.Models.Post(@get('post')))

setPost: (post) ->
@post = post

class Backbonerelationaldemo.Collections.CommentsCollection extends Backbone.Collection
model: Backbonerelationaldemo.Models.Comment
url: '/comments'

最佳答案

我最近处理了同样的问题。它实际上不是 HashWithIndifferentAccess 错误:它与 accepts_nested_attributes_for 期望参数的方式有关。

当您声明 accepts_nested_attributes_for :comments 时,Rails 会在传入的参数上查找参数调用 comments_attributes

问题是来自 Backbone 的 JSON 表示具有 "comments" 属性,而不是 "comments_attributes" 属性。

您可以通过向您的 Post 模型添加一个 toJSON 函数来在 Backbone 端修复它:

# in your Post model
toJSON: ->
attrs = _.clone(@attributes)
attrs.comments_attributes = _.clone(@attributes.comments)
delete attrs.comments
attrs

或者您可以在 Rails Controller 中处理它:

# in your Posts controller
def update
params[:comments_attributes] = params.delete(:comments) if params.has_key? :comments
# call to update_attributes and whatever else you need to do
end

希望这对您有所帮助。

关于javascript - 主干和 Rails 关联 : Avoiding JSON HashWithIndifferentAccess errors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8715682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com