【发布时间】:2015-03-19 17:56:21
【问题描述】:
我正在使用嵌套属性和强参数,我已经看到了在强参数中嵌套属性的两种不同方式。
举个例子:
class Post < ActiveRecord::Base
has_many :comments
accepts_nested_attributes, :comments
class Comments < ActiveRecord::Base
belongs_to :post
在Post Controller中看到了这两种cmet嵌套参数的方式,有区别吗?
params.require(:post)
.permit(:title, :id, :author, :comment => [:id, :content, :post_id, :user_id])
或
params.require(:post)
.permit(:title, :id, :author, comments_attributes: [:id, :content, :post_id, :user_id])
它们看起来很相似......通常我会认为一个是早期版本,但我相信强参数是相对较新的。有什么区别?
【问题讨论】:
-
从未见过它们被这样使用。第二个是应该的。它们都有效吗?
-
我只用过第二个成功。我在这里获得了代码,因此使用了第一个代码并且代码不起作用。我太新了,看不出差异是否与参数、我的无知或新代码有关。此外,这个答案使用第一个......虽然它实际上可能不是nested_attributes:stackoverflow.com/questions/18436741/…
标签: ruby-on-rails ruby-on-rails-4 nested-attributes strong-parameters