aperture science
2010-Nov-07 20:56 UTC
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.
--
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.
aperture science
2010-Nov-08 08:54 UTC
Re: having trouble accessing a nested resource in rails 2.3.5
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.
On Nov 7, 12:56 pm, aperture science
<mekkagoj...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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.
--
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.
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 On Nov 7, 2:56 pm, aperture science <mekkagoj...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.-- 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.