<tfoot id='Nisge'></tfoot>
    1. <i id='Nisge'><tr id='Nisge'><dt id='Nisge'><q id='Nisge'><span id='Nisge'><b id='Nisge'><form id='Nisge'><ins id='Nisge'></ins><ul id='Nisge'></ul><sub id='Nisge'></sub></form><legend id='Nisge'></legend><bdo id='Nisge'><pre id='Nisge'><center id='Nisge'></center></pre></bdo></b><th id='Nisge'></th></span></q></dt></tr></i><div id='Nisge'><tfoot id='Nisge'></tfoot><dl id='Nisge'><fieldset id='Nisge'></fieldset></dl></div>

        • <bdo id='Nisge'></bdo><ul id='Nisge'></ul>
        <legend id='Nisge'><style id='Nisge'><dir id='Nisge'><q id='Nisge'></q></dir></style></legend>
      1. <small id='Nisge'></small><noframes id='Nisge'>

      2. Backbone 和 Rails 关联:避免 JSON HashWithIndifferentAccess 错误

        Backbone and Rails associations: Avoiding JSON HashWithIndifferentAccess errors(Backbone 和 Rails 关联:避免 JSON HashWithIndifferentAccess 错误)
              <tbody id='PwPQd'></tbody>
                  <bdo id='PwPQd'></bdo><ul id='PwPQd'></ul>

                • <i id='PwPQd'><tr id='PwPQd'><dt id='PwPQd'><q id='PwPQd'><span id='PwPQd'><b id='PwPQd'><form id='PwPQd'><ins id='PwPQd'></ins><ul id='PwPQd'></ul><sub id='PwPQd'></sub></form><legend id='PwPQd'></legend><bdo id='PwPQd'><pre id='PwPQd'><center id='PwPQd'></center></pre></bdo></b><th id='PwPQd'></th></span></q></dt></tr></i><div id='PwPQd'><tfoot id='PwPQd'></tfoot><dl id='PwPQd'><fieldset id='PwPQd'></fieldset></dl></div>
                  <tfoot id='PwPQd'></tfoot>
                  <legend id='PwPQd'><style id='PwPQd'><dir id='PwPQd'><q id='PwPQd'></q></dir></style></legend>
                • <small id='PwPQd'></small><noframes id='PwPQd'>

                • 本文介绍了Backbone 和 Rails 关联:避免 JSON HashWithIndifferentAccess 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  I'm trying to get my backbone associations working inside a rails app , and I'm having difficulty when trying to update existing models. Specifically, Rails throws the following error:

                  在 2012-01-04 02:36:14 +1000 开始为 127.0.0.1 放置/posts/2"
                  PostsController#update 处理为 JSON 参数:{"post"=>{"content"=>"Seconderona","created_at"=>"2012-01-03T10:51:09Z", "id"=>2, "title"=>"第二次测试发布",updated_at"=>2012-01-03T10:51:09Z",评论"=>[{}]},"id"=>"2"} Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE帖子".id"=?LIMIT 1 [["id", "2"]] 警告:不能批量分配受保护的属性:id Completed 500 Internal Server Error in 15ms

                  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 (评论(#70104367824560)预期,得到ActiveSupport::HashWithIndifferentAccess(#70104367278120)):
                  app/controllers/posts_controller.rb:62:in block in update'
                  app/controllers/posts_controller.rb:61:in
                  update'

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

                  一些事情:

                  这是触发的(例如):

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

                  另外,是的,模型中存在accepts_nested_attributes_for".

                  Also, yes, 'accepts_nested_attributes_for' is present in the model.

                  下面的(有问题的)代码几乎一字不差地取自 thougtbot 的backbone on rails"电子书,我还尝试遵循骨干关系 gem 的文档.两者都会引发此错误.任何想法表示赞赏,下面的代码

                  The (offending) code below is taken pretty much verbatim from thougtbot's "backbone on rails" ebook, and I've also tried following the documentation for the backbone-relational gem. Both raise this error. Any ideas appreciated, code below

                  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
                  

                  铁路评论"模型

                  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
                  

                  骨干后控制器

                  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'
                  

                  主干评论控制器

                  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 期望参数的方式有关.

                  I dealt with the same issue recently. It's actually not a HashWithIndifferentAccess error: it has to do with how accepts_nested_attributes_for expects params.

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

                  When you declare accepts_nested_attributes_for :comments, Rails looks for a parameter call comments_attributes on the incoming params.

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

                  The problem is that your JSON representation coming from Backbone has a "comments" property instead of a "comments_attributes" property.

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

                  You could fix it on the Backbone side by adding a toJSON function to your Post model:

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

                  或者您可以在 Rails 控制器中处理它:

                  Or you could handle it in your 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
                  

                  希望这会有所帮助.

                  这篇关于Backbone 和 Rails 关联:避免 JSON HashWithIndifferentAccess 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                  How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社
                  How to use coffeescript in developing web-sites?(如何在开发网站时使用coffeescript?)
                • <i id='b2sN9'><tr id='b2sN9'><dt id='b2sN9'><q id='b2sN9'><span id='b2sN9'><b id='b2sN9'><form id='b2sN9'><ins id='b2sN9'></ins><ul id='b2sN9'></ul><sub id='b2sN9'></sub></form><legend id='b2sN9'></legend><bdo id='b2sN9'><pre id='b2sN9'><center id='b2sN9'></center></pre></bdo></b><th id='b2sN9'></th></span></q></dt></tr></i><div id='b2sN9'><tfoot id='b2sN9'></tfoot><dl id='b2sN9'><fieldset id='b2sN9'></fieldset></dl></div>
                    <tbody id='b2sN9'></tbody>

                    <small id='b2sN9'></small><noframes id='b2sN9'>

                      • <bdo id='b2sN9'></bdo><ul id='b2sN9'></ul>

                          <legend id='b2sN9'><style id='b2sN9'><dir id='b2sN9'><q id='b2sN9'></q></dir></style></legend>

                          • <tfoot id='b2sN9'></tfoot>