I have a model called comment which has the typical stuff like belongs_to :post, author, email, body and a posts controller with the method add_comment. On the views/posts/show.html.erb I have: <% form_for @comment, :url => {:action => :add_comment} do |f| %> <div>Name: <%= f.text_field :author%></div> <div>Email: <%= f.text_field :email%></div> <div><%= f.text_area :body%></div> <div><%= f.submit ''submit'' %></div> <% end %> How do I wire it up so that when I''m on localhost:3000/posts/24/ and hit the submit button, it will either: A) Add a new comment with the right post_id B) Redirect back to localhost:3000/posts/24/ with an error message if all required fields are not filled out. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Look into making comments a nested resource of your posts. This will keep your comments in the context of a post. Your route would look something like: /posts/24/comments Using a POST on that URI would call your comments_controller create action. On Jul 31, 10:55 am, et <erict...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a model called comment which has the typical stuff like > belongs_to :post, author, email, body and a posts controller with the > method add_comment. > > On the views/posts/show.html.erb I have: > > <% form_for @comment, :url => {:action => :add_comment} do |f| %> > <div>Name: <%= f.text_field :author%></div> > <div>Email: <%= f.text_field :email%></div> > <div><%= f.text_area :body%></div> > <div><%= f.submit ''submit'' %></div> > <% end %> > > How do I wire it up so that when I''m on localhost:3000/posts/24/ and > hit the submit button, it will either: > A) Add a new comment with the right post_id > B) Redirect back to localhost:3000/posts/24/ with an error message if > all required fields are not filled out.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Wow thanks for the quick response. I''ve tried the nested routes route. (map.resources :posts, :has_many => :comments) I need /posts/24/comments to look exactly the same as /posts/24, except with error messages for validating the comment fields. However, I get this weird artifact where the comment I filled out partially shows up on /posts/24/comments. Also, comments only needs add and delete so I figured I should make it a model without a controller and just have posts_controller do the add and admin_controller do the delete. Is there a way to achieve this? On Jul 31, 11:09 am, Robert Walker <r0b3rt4...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Look into making comments a nested resource of your posts. This will > keep your comments in the context of a post. > > Your route would look something like: > /posts/24/comments > > Using a POST on that URI would call your comments_controller create > action. > > On Jul 31, 10:55 am, et <erict...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I have a model called comment which has the typical stuff like > > belongs_to :post, author, email, body and a posts controller with the > > method add_comment. > > > On the views/posts/show.html.erb I have: > > > <% form_for @comment, :url => {:action => :add_comment} do |f| %> > > <div>Name: <%= f.text_field :author%></div> > > <div>Email: <%= f.text_field :email%></div> > > <div><%= f.text_area :body%></div> > > <div><%= f.submit ''submit'' %></div> > > <% end %> > > > How do I wire it up so that when I''m on localhost:3000/posts/24/ and > > hit the submit button, it will either: > > A) Add a new comment with the right post_id > > B) Redirect back to localhost:3000/posts/24/ with an error message if > > all required fields are not filled out.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---