Ruby on rails 设计嵌套形式,有很多关系
app/models/post.rbRuby on rails 设计嵌套形式,有很多关系,ruby-on-rails,devise,Ruby On Rails,Devise,app/models/post.rb class User < ActiveRecord::Base has_many :posts, :dependent => :destroy accepts_nested_attributes_for :posts attr_accessible ... :posts_attributes ... class Post < ActiveRecord::Base belongs_to :user has_one :u
class User < ActiveRecord::Base
has_many :posts, :dependent => :destroy
accepts_nested_attributes_for :posts
attr_accessible ... :posts_attributes
...
class Post < ActiveRecord::Base
belongs_to :user
has_one :user
attr_accessible :title
end
其一,用户注册作品的形式。但是post部分不会显示在当前配置中。如果我将:post的字段切换为:post的字段,我会得到完整的表单,但在提交时会出现错误“无法批量分配受保护的属性”
现在,如果我将:post添加到attr_accessible,就会得到错误“unknown parameter post”
如果要将其移动到用户控制器,如何覆盖默认的设计控制器?这有关系吗?你也必须在你的
Post
模型中定义你的attr\u accessible
app/models/post.rb
class User < ActiveRecord::Base
has_many :posts, :dependent => :destroy
accepts_nested_attributes_for :posts
attr_accessible ... :posts_attributes
...
class Post < ActiveRecord::Base
belongs_to :user
has_one :user
attr_accessible :title
end
希望这会有所帮助。谢谢我在我的表单之前添加了,这很有效。我在attr_accessible中有Post的所有属性。我已经更新了答案。试试这个,让我知道它是否有效。我添加到我的视图中,这很有效
class Post < ActiveRecord::Base
belongs_to :user
has_one :user
attr_accessible :title
end
resource.posts.build