0

I have 3 models one

Customer               Book                 Book_Managers
id                     id                   id
first                  description          customer_id
last                   book_manager_id      visible
email                   
password                 

When the customer goes at customer#edit it then render the form books and book_managers into one form

In my bookController I have the following to create a book and 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

But then it tell me there is missing template. I am also only using a simple form_for. What i am doing wrong? I believe I should be using accepts_nested_attributes_for but any one has a good tutorial for this??

Update: In regard to the railcasts here my relationship and see if it make sense?

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
4

2 に答える 2

0

Well, most of the time, after a successful create, you should redirect_to your edit or listing page with a success message (flash[:info] = "Success").

So you should have

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

And in the view display <%= flash[:info] %> in green for example :)

于 2012-08-02T13:40:56.097 に答える
0

Have a look at these railscasts http://railscasts.com/episodes/196-nested-model-form-part-1 and http://railscasts.com/episodes/197-nested-model-form-part-2. They will show you the way to correctly implement nested forms.

于 2012-08-02T13:41:33.743 に答える