Hello folks!
Beare with me for a second, while I explain my problem.
Assuming we have the trivial model of
class Author
has_many :books
end
class Book
end
How do people go about creating the authors/edit and authors/new
views if you want to be able to add and remove arbitrary amount of
Books at Author creation and edit time? What I mean by this is that I
go to ‘authors/new’ then add a few Books and then create the Author.
Let’s assume that I am using link_to_remote for the on the fly
addition of Books to the Author. It is important that neither the
Author nor the Books are saved to the database until the Create form
button is pressed.
My solution so far is to use the session object to keep an Array with
new Books that are not saved in the database.
I have a books/_book.rhtml partial
<p><%= text_field_tag "books[#{book.id}][title]", book.title %></p>
…an authors/_form.rhtml partial
<%= text_field "author", "name" %>
<%= link_to_remote "Add book", :url => {:action => "add_book"}
:update => "books_list" %>
<div id="books_list">
<%= render :partial => "books/book", :collection => author.books %>
</div>
…and in authors_controller.rb
def new
session[:books] = Array.new
end
def add_book
book = Book.new(:title => "untitled")
book.id = -session[:books].size
session[:books] << book
render :partial => "books/book", :collection => session[:books]
end
def create
# code ommited but..
# I look for session[:books] and iterate through each adding a new
one if it has a
# negative id, otherwise find the exisiting book (when coming from
an edit)
end
The above approach breaks down when adding books because book.id is
not defined yet. The ugly hack is that I am setting the id to a
negative value so that I can then spot it in my create method and
create proper Book objects. The above works for the edit mode.
This gets even messier if you have Chapters and a Book has_many
chapters. There seems to be no recursive way of creating and then
parsing all these.
-christos