【发布时间】:2015-01-21 01:23:38
【问题描述】:
我刚刚将一个旧应用从 Rails 3.2.7 升级到 3.2.21,从 Ruby 1.9.3 升级到 2.0.0,在试用该应用时出现以下错误:
Started PUT "/books/17" for 127.0.0.1 at 2015-01-20 16:15:10 -0800
Processing by BooksController#update as HTML
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"mXizjcxG5Yq0BYG9vvvuqjW6O3/KhG6fUkoEeU6ORP8=",
"book"=>{"new_book_item_attributes"=>{"1421799301.062898"=>{"content_id"=>"16"}}},
"commit"=>"Add selected content", "id"=>"17"}
ActiveRecord::UnknownAttributeError (unknown attribute: new_book_item_attributes):
app/controllers/books_controller.rb:70:in `block in update'
app/controllers/books_controller.rb:69:in `update'
这是我的相关代码:
books_controller.rb:69
if @book.update_attributes(params[:book])
book.rb
class Book < ActiveRecord::Base
attr_accessible ... :new_book_item_attributes
def new_book_item_attributes=(attributes)
attributes.each do |attribute|
book_items.build(skip_over_time(attribute))
end
end
我浏览了http://weblog.rubyonrails.org/releases/ 中的每个博客条目,从 Rails 3.2.8.rc1 已发布! 到 Rails 3.2.21、4.0.12 和 4.1.8 已发布发布了,我没有发现任何可以解释这个问题的激烈的东西。无论如何,这是一个“补丁级”升级,所以我不希望有任何剧烈的变化。知道可能有什么问题吗?
更新
- Rails
3.2.21和 Ruby1.9.3不会出现此问题(它只会出现在 Ruby2.0.0上(不管 Rails 是什么(3.2.21或3.2.7))。
我将@xavier-shay标识的代码块修改如下:
puts "-1- attributes: #{attributes.inspect}"
attributes.each do |k, v|
puts "-2- testing k: #{k}, v: #{v}"
if k.include?("(")
puts "-3- true to k.include?(\"(\")"
multi_parameter_attributes << [ k, v ]
elsif respond_to?("#{k}=")
puts "-4- true to respond_to?(\"#{k}=\")"
if v.is_a?(Hash)
puts "-5- true to v.is_a?(Hash)"
nested_parameter_attributes << [ k, v ]
else
puts "-6- else"
send("#{k}=", v)
end
else
puts "-7- UnknownAttributeError"
raise(UnknownAttributeError, "unknown attribute: #{k}")
end
end
在 Ruby 2.0.0 和 Rails 3.2.21 上运行它会引发上述参数的异常:
-1- attributes: {"new_book_item_attributes"=>{"1421825320.571702"=>{"content_id"=>"8"}}}
-2- testing k: new_book_item_attributes, v: {"1421825320.571702"=>{"content_id"=>"8"}}
-7- UnknownAttributeError
...但是,在另一个控制器中,(显然使用不同的参数)没有问题:
-1- attributes: {"title"=>"test title", "subtitle"=>"test subtitle"}
-2- testing k: title, v: test title
-4- true to respond_to?("title=")
-6- else
-2- testing k: subtitle, v: test subtitle
-4- true to respond_to?("subtitle=")
-6- else
在 Ruby 1.9.3 和 Rails 3.2.21 上运行没有问题:
-1- attributes: {"new_book_item_attributes"=>{"1421828430.9315462"=>{"content_id"=>"8"}}}
-2- testing k: new_book_item_attributes, v: {"1421828430.9315462"=>{"content_id"=>"8"}}
-4- true to respond_to?("new_book_item_attributes=")
-5- true to v.is_a?(Hash)
-1- attributes: {"content_id"=>"8"}
-2- testing k: content_id, v: 8
-4- true to respond_to?("content_id=")
-6- else
-1- attributes: {"book_id"=>17}
-2- testing k: book_id, v: 17
-4- true to respond_to?("book_id=")
-6- else
这个推论指向 Ruby,而不是 Rails。关于我应该如何进一步调试的任何想法?
【问题讨论】:
-
发布错误发生的整个动作怎么样?
-
如果为 new_book_item_attributes 添加一个空的 getter 会发生什么?
-
感谢您的 cmets。我用我做的一些调试更新了我的问题。目前看来 Ruby 是问题所在——尽管我不确定如何继续调试。
-
您的两个控制器的属性之间有两个区别:1)嵌套值-->应该没关系,因为它是问题的关键。 2)蛇案名v.单名;或...长名称与短名称。您可以尝试将虚拟属性的名称更改为
x吗? -
我将虚拟属性更改为
xxx,问题是相同的。再次感谢您的帮助。
标签: ruby-on-rails ruby activerecord ruby-1.9.3 ruby-2.0