Having trouble accessing a nested resource in rails 2.3.5

I am trying to add comments to my blog.

comments_controller.rb:
def create
@blog = Blog.find(params[:blog_id])
@comment = @blog.comments.create(params[:comment])
redirect_to blog_path(@blog)
end

In my blog’s index view:
<% form_for([ @blog, @comment ]) do |f| %>

<% end %>

blog.rb:
has_many :comments

comment.rb:
belongs_to :blog

routes.rb:
map.resources :blogs, :has_many => :comments

The error I get is:
undefined method ‘nil_class_path’
and it points to the form_for line

I can add comments from the console, but if I attempt to view them by
adding “@comments = Blog.comments.all” to my blog_controller’s index
action, then "<% @comments.each do |comment| %> " into the
corresponding view, I get an “undefined method ‘comments’” error

Any assistance would be awesome. And I am only using rails 2 because
my host does not support rails 3. I do not have any trouble getting
the above to work in rails 3, but i don’t know what I am doing wrong
now.

Thanks.

I managed to make it work well enough.

I put the following into the blog controller:
def show
@blog = Blog.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.xml { render :xml => @blog }
end

end

then used ajax to load the commends from the show URL into the
index…probably not the sanest way to do it, but i kept getting
errors otherwise.

I believe your problem is this:

Blog.comments.all

You want comments on a specific blog, so you need this instead:

@blog = Blog.find(params[:id])
@comments = @blog.comments

Hope this helps.

Jeff

purpleworkshops.com