我有3个模特一个
Customer Book Book_Managers
id id id
first description customer_id
last book_manager_id visible
email
password
当客户进入客户#编辑时,然后将表格书籍和book_managers呈现为一种形式
在我的bookController中,我有以下内容来创建一本书和book_manager
class BooksController < ApplicationController
def create
@customer = current_customer
@book = @customer.book_managers.build(:visible => "true")
@book = @customer.book_managers.first.books.build(:description => "2012")
end
end
然后它告诉我缺少模板。我也只使用一个简单的form_for。我做错了什么?我相信我应该使用accepts_nested_attributes_for,但是任何人都有一个很好的教程吗?
更新:关于铁路广播,我的关系,看看它是否有意义?
Customer
has_many :book_managers
has_many :books, :through => :book_managers
accepts_nested_attributes_for :book_manager
Book
belongs_to :book_manager
def customer
book_manager.customer
end
Book_Manager
belongs_to :customer
has_many :books
accepts_nested_attributes_for :book
答案 0 :(得分:0)
好吧,大多数情况下,在成功创建之后,您应redirect_to
在您的编辑或列表页面中显示成功消息(flash[:info] = "Success"
)。
所以你应该
def create
@customer = current_customer
@book = @customer.book_managers.build(:visible => "true")
@book = @customer.book_managers.first.books.build(:description => "2012")
flash[:info] = "Success"
redirect_to :some_action
end
在视图中以绿色显示<%= flash[:info] %>
,例如:)
答案 1 :(得分:0)
查看这些railscast http://railscasts.com/episodes/196-nested-model-form-part-1和http://railscasts.com/episodes/197-nested-model-form-part-2。它们将向您展示正确实现嵌套表单的方法。