# Nested Resourced, Books \> Authorsâ€¦ Creatin g Records for Authorsâ€¦

**URL:** <https://www.ruby-forum.com/t/nested-resourced-books-authorsa-creatin-g-records-for-authorsa/195915>\
**Category:** Rails\
**Created:** [October 2, 2010, 8:49pm UTC](https://www.ruby-forum.com/t/nested-resourced-books-authorsa-creatin-g-records-for-authorsa/195915 "2010-10-02T20:49:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [October 2, 2010, 8:49pm UTC](https://www.ruby-forum.com/t/nested-resourced-books-authorsa-creatin-g-records-for-authorsa/195915/1 "2010-10-02T20:49:01Z")

</div>

Hello,

I have the following Rails3 nested resource in my routes.rb:

```
resources :books do
  resources :authors
end

```

My authors table has ID | name | book\_id

Given I’m at the URL /books/312/authors/new

I’m having problems getting a new author created for the book…

Here’s my authors controller code:

def create  
@books = Book.find(params[:book\_id])  
@author = @book.authors.create(params[:note])  
.  
.  
.  
end

My NEW FORM (which I think is the problem, looks like this:

\<%=form\_for [@book , @author] do |f| %\>  
.  
.  
.

 \<%= f.submit %\> 
\<% end %\>

When I submit I get the ERROR:  
“Couldn’t find Book without an ID”  
And the URL changes to "/books/ & NOT /books/312/authors … as  
expected

Suggestions? Thank you

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [October 2, 2010, 9:11pm UTC](https://www.ruby-forum.com/t/nested-resourced-books-authorsa-creatin-g-records-for-authorsa/195915/2 "2010-10-02T21:11:51Z")

</div>

Updated the author controller with:

def new  
@note = Author.new  
@book = Book.find(params[:book\_id])  
.  
.  
end

Now the form is creating what seems like the right URL:

...

But after submitting a new author, it errors with (after hanging for a  
while locally):  
SQLite3::BusyException: database is locked: INSERT INTO  
“authors” …

thanks

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [October 2, 2010, 10:59pm UTC](https://www.ruby-forum.com/t/nested-resourced-books-authorsa-creatin-g-records-for-authorsa/195915/3 "2010-10-02T22:59:32Z")

</div>

On Sat, Oct 2, 2010 at 8:47 PM, nobosh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Given I’m at the URL /books/312/authors/new
> 
> I’m having problems getting a new author created for the book…
> 
> Here’s my authors controller code:
> 
> def create  
> @books = Book.find(params[:book\_id])  
> @author = @book.authors.create(params[:note])  
> .

this should look like this

def create  
@books = Book.find(params[:book\_id])  
@author = @book.authors.build(params[:author])  
if @autho.save  
blah blah …

what you have makes no sense

> 

for this to work in the new action in the controller you must have  
created  
@book and @author like this

def new

@book = Book…find(params[:book\_id])  
@author = @book.authors.new

if you dont then this [@book , @author] gets book as nil

> expected

post what you do after save, it appears something is wrong in the render  
:action =\> :new section of the create action

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [October 2, 2010, 11:02pm UTC](https://www.ruby-forum.com/t/nested-resourced-books-authorsa-creatin-g-records-for-authorsa/195915/4 "2010-10-02T23:02:27Z")

</div>

On Sat, Oct 2, 2010 at 9:10 PM, nobosh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Updated the author controller with:
> 
> def new  
> @note = Author.new

What is this? what is note? makes no sense

Now the form is creating what seems like the right URL:

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [October 2, 2010, 11:39pm UTC](https://www.ruby-forum.com/t/nested-resourced-books-authorsa-creatin-g-records-for-authorsa/195915/5 "2010-10-02T23:39:19Z")

</div>

Thanks radhames - that did the trick!

I now have a view here: /books/131/authors/  
And I want to each record to link to something like: /books/131/  
authors/3333

```
<% @authors.each do |author| %>
   <%= link_to 'author.name', book_author_path(@book, @author) %>
<% end %>

```

but that error’s with: No route matches  
{:action=\>“destroy”, :controller=\>“authors”}

I also tried:

```
<%= link_to 'author.name', [@book, author] %>

```

Problem is the code keeps linking to /authors/3333, not /books/131/  
authors/3333

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [October 3, 2010, 12:34am UTC](https://www.ruby-forum.com/t/nested-resourced-books-authorsa-creatin-g-records-for-authorsa/195915/6 "2010-10-03T00:34:20Z")

</div>

use polymorphic\_path()

or

[@book, @author]

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [October 3, 2010, 1:55am UTC](https://www.ruby-forum.com/t/nested-resourced-books-authorsa-creatin-g-records-for-authorsa/195915/7 "2010-10-03T01:55:41Z")

</div>

got it - thanks!
