# Display a form with selected values from URL

**URL:** <https://www.ruby-forum.com/t/display-a-form-with-selected-values-from-url/150443>\
**Category:** Rails\
**Created:** [October 8, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/display-a-form-with-selected-values-from-url/150443 "2008-10-08T17:30:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![robertderosny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9a140/32.png) [@robertderosny](https://www.ruby-forum.com/u/robertderosny)\
**Post date:** [October 8, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/display-a-form-with-selected-values-from-url/150443/1 "2008-10-08T17:30:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![robertderosny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9a140/32.png) [@robertderosny](https://www.ruby-forum.com/u/robertderosny)\
**Post date:** [October 8, 2008, 7:26pm UTC](https://www.ruby-forum.com/t/display-a-form-with-selected-values-from-url/150443/2 "2008-10-08T19:26:28Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![robertderosny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9a140/32.png) [@robertderosny](https://www.ruby-forum.com/u/robertderosny)\
**Post date:** [October 9, 2008, 2:49am UTC](https://www.ruby-forum.com/t/display-a-form-with-selected-values-from-url/150443/3 "2008-10-09T02:49:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![robertderosny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9a140/32.png) [@robertderosny](https://www.ruby-forum.com/u/robertderosny)\
**Post date:** [October 9, 2008, 4:02am UTC](https://www.ruby-forum.com/t/display-a-form-with-selected-values-from-url/150443/4 "2008-10-09T04:02:32Z")

</div>

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.
