0
我知道我的標題是一個繞口令,所以我會盡我所能解釋發生了什麼事情。在同一模型上嵌套模型的巢模型形式,嵌套模型... Rails 3
首先,我有一個模型。讓我們把它稱爲帖子。
然後我有第二個模型,它基本上是一個連接表,將兩個不同的帖子鏈接在一起。我們可以調用該帖子連接。
我按照這裏的說明完成了這項工作: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創建管理員手動連接後,他們的工作好了。
現在的問題是,我想製作一個表格,其中帖子連接嵌套在帖子下。
我一直努力遵循導軌投: http://railscasts.com/episodes/73-complex-forms-part-1
但即使是前幾步沒有工作。我沒有收到錯誤。沒有任何東西出現在田地應該填充的地方。 (我想知道,如果它與做:
3.times { @post.post_connections.build }
)
有,我應該根據模型來要對此更復雜的方法嗎?
您是否錯過了`<= =`中的`=`?在Rails 3中,你需要使用`=`來表示`form_form`和`fields_for`。 – Zabba 2011-01-29 12:37:40