display a form with selected values from URL

Hi there,

I have an author model and a book model.

I'm trying to create a "add new book" link for each author from the authors index page, that would point to a new book page, with the the author select field already "selected" to the good option. the author select in the new book form is created with collection_select().

I don't know how to pass such a variable in a url using rails url schema. I've looked in the link_to documentation, but nothing about this specific case.

any help would be greatly appreciated, thanks in advance.

Two ways the good and the bad :

Good:

url: /author/1/book/new

BookController new @author = Author.find(params[:author_id]) @book = @author.books.new

or (not so good)

BookController

@author = Author.find(params[:author]) @book = Book.new @book.author = @author

Should work too...

thanks for your help

/authors/3/books/new gives me a

Routing Error No route matches "/authors/3/books/new" with {:method=>:get}

I guess i need to update my routes.rb for this to work ?

i've tried this :

map.resources :books, :path_prefix => "/authors/:author_id"

without success ..

if that helps : i'm using rails 2 with REST.

I've managed to do it the old way using /books/new?author_id= and @author = Author.find(params[:author_id]) @book = @author.books.new as you've suggested.

thanks again.