Forum: Ruby on Rails display a form with selected values from URL

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3caa9e9d139143e8b23dc767da6a33bc?d=identicon&s=25 robertderosny (Guest)
on 2008-10-08 17:30
(Received via mailing list)
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.
4c438a80fc30661ad619ea177cf9cbd0?d=identicon&s=25 Freddy Andersen (Guest)
on 2008-10-08 19:26
(Received via mailing list)
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...
3caa9e9d139143e8b23dc767da6a33bc?d=identicon&s=25 robertderosny (Guest)
on 2008-10-09 02:49
(Received via mailing list)
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.
3caa9e9d139143e8b23dc767da6a33bc?d=identicon&s=25 robertderosny (Guest)
on 2008-10-09 04:02
(Received via mailing list)
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.
This topic is locked and can not be replied to.