nobosh
2010-Oct-02 18:47 UTC
[Rails] Nested Resourced, Books > Authors… Creating Records for Authors…
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| %> . . . <div class="actions"> <%= f.submit %> </div> <% 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
nobosh
2010-Oct-02 19:10 UTC
[Rails] Re: Nested Resourced, Books > Authors… Creating Records for Authors…
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: <form accept-charset="UTF-8" action="/books/3/authors" class="new_author" id="new_author" method="post"> ... But after submitting a new author, it errors with (after hanging for a while locally): SQLite3::BusyException: database is locked: INSERT INTO "authors" ......... thanks On Oct 2, 11:47 am, nobosh <bhellm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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| %> > . > . > . > > <div class="actions"> > <%= f.submit %> > </div> > <% 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-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
radhames brito
2010-Oct-02 20:58 UTC
Re: [Rails] Nested Resourced, Books > Authors… Creating Records for Authors…
On Sat, Oct 2, 2010 at 8:47 PM, nobosh <bhellman1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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]) > . >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> . > . > end > > My NEW FORM (which I think is the problem, looks like this: > > <%=form_for [@book , @author] do |f| %> > . > . > . >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> > <div class="actions"> > <%= f.submit %> > </div> > <% 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 >post what you do after save, it appears something is wrong in the render :action => :new section of the create action> > Suggestions? Thank you > > ---- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
radhames brito
2010-Oct-02 21:01 UTC
Re: [Rails] Re: Nested Resourced, Books > Authors… Creating Records for Authors…
On Sat, Oct 2, 2010 at 9:10 PM, nobosh <bhellman1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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:> <form accept-charset="UTF-8" action="/books/3/authors" > class="new_author" id="new_author" method="post"> > ... > > the action path is not rigth-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
nobosh
2010-Oct-02 21:37 UTC
[Rails] Re: Nested Resourced, Books > Authors… Creating Records for Authors…
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 On Oct 2, 2:01 pm, radhames brito <rbri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sat, Oct 2, 2010 at 9:10 PM, nobosh <bhellm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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: > > > > > <form accept-charset="UTF-8" action="/books/3/authors" > > class="new_author" id="new_author" method="post"> > > ... > > > the action path is not rigth-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
radhames brito
2010-Oct-02 22:33 UTC
Re: [Rails] Re: Nested Resourced, Books > Authors… Creating Records for Authors…
use polymorphic_path() or [@book, @author] -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
nobosh
2010-Oct-02 23:54 UTC
[Rails] Re: Nested Resourced, Books > Authors… Creating Records for Authors…
got it - thanks! On Oct 2, 3:33 pm, radhames brito <rbri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> use polymorphic_path() > > or > > [@book, @author]-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.