【问题标题】:Nest model form with many to many relationship on same model, in nested model...Rails 3嵌套模型形式在同一模型上具有多对多关系,在嵌套模型中......Rails 3
【发布时间】:2011-01-29 09:22:02
【问题描述】:

我知道我的标题是绕口令,所以我会尽力解释发生了什么。

首先,我有一个模型。让我们称之为帖子。

然后我有第二个模型,它本质上是一个连接表,将两个不同的帖子链接在一起。我们可以称之为帖子连接。

我按照此处的说明完成此操作:Many-to-many relationship with the same model in rails? 用于单向,带有附加字段。

这些模型的代码如下所示:

class PostConnection < ActiveRecord::Base
  belongs_to :post_a, :class_name => :Post
  belongs_to :post_b, :class_name => :Post
end

class Post < ActiveRecord::Base
  has_many(:post_connections, :foreign_key => :post_a_id, :dependent => :destroy)
  has_many(:reverse_post_connections, :class_name => :PostConnection,
      :foreign_key => :post_b_id, :dependent => :destroy)

  has_many :posts, :through => :post_connections, :source => :post_b
  accepts_nested_attributes_for :post_connections
end

到目前为止,这工作正常 - 当我使用 rails admin 手动创建帖子连接时,它们工作得很好。

现在的问题是,我想制作一个表单,其中包含嵌套在帖子下的帖子连接。

我一直在尝试遵循轨道演员: http://railscasts.com/episodes/73-complex-forms-part-1

但即使是最初的几个步骤也没有奏效。我没有收到错误。没有出现在字段应该填充的位置。 (我想知道它是否与:

  3.times { @post.post_connections.build }

)

有没有更复杂的方法可以让我根据模型来解决这个问题?

【问题讨论】:

  • 你错过了&lt;%= 中的= 吗?在 Rails 3 中,您需要将= 用于form_formfields_for
  • 嘿,扎巴,就是这样!请在答案中回复,以便您获得积分! :)

标签: ruby-on-rails ruby-on-rails-3


【解决方案1】:

你错过了

:D

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    相关资源
    最近更新 更多