Welcome Guest | Login

Restful website error

Hi there,

I have set up my blog application and have server rendering main controller properly.

I have comment controller renders a form to leave comments:

<% form_for :comment, :url => comments_path(@entry) do |f| %>
...

in my routes.rb, I have specified the resource:

map.resources :entries do |entries|
   # /connments/1/add_comment
   entries.resources  :comments
 end

so when I invoke add_comment action, it should lead to domain/stage/id/add_comment

It works properly on my local machine:

Processing StageController#add_comment (for 127.0.0.1 at 2007-08-05 12:32:01) [POST]
 Session ID: 6297be32d59763815af7b57e00c01732
 Parameters: {"action"=>"add_comment", "id"=>"8", "controller"=>"stage"}
 Entry Columns (0.002143)   SHOW FIELDS FROM entries
 Entry Load (0.000554)   SELECT * FROM entries WHERE (entries.`id` = 8)
Rendering  within layouts/stage
Rendering stage/add_comment
Completed in 0.01506 (66 reqs/sec) | Rendering: 0.00551 (36%) | DB: 0.00270 (17%) | 200 OK http://localhost/stage/add_comment/8]


However the server generates the following error :

Processing StageController#add_comment (for 68.145.102.32 at 2007-08-05 13:27:28) [POST]
 Session ID: 85a8e69db896dca20737a92996a9904d
 Parameters: {"action"=>"add_comment", "id"=>"1", "controller"=>"stage"}
Rendering  within layouts/stage
Rendering stage/add_comment


ActionView::TemplateError (undefined method `comments_path' for #<#<Class:0xb7426bcc>:0xb759d71c>) on line #1 of app/views/stage/add_comment.rhtml:
1: <% form_for :comment, :url => comments_path(@entry) do |f| %>
2:      <p>
3:      <label for="comment_name"><p>Name: (required)</p></label>
4:      <%= f.text_field :name %>

any thoughts?

thanks!

2007-08-05 02:10 PM

A few things to try:

map.resources :entries do |entry|
  entry.resources  :comments
end

or try:

<% form_for :comment, @comment, :url => comments_path(@entry) do |f| %>

or maybe:

<% form_for :comment, @comment, :url => comments_url(@entry) do |f| %>

2007-08-05 03:38 PM

I have it figured out, thanks!

2007-08-06 02:02 AM

If you're having trouble with this, it could be for a variety of reasons. The easiest way to resolve this issue is to follow the RESTful routes.txt instructions I lined out here.

2008-03-22 12:16 AM


Hello Guest! In order to post you must be an active client with us, please log in or sign up.