Hi,
So I''m still trying to work through a tutorial on building a CMS and
blog. I have a blog_controller.rb with post and comments. In my show
action, I''m trying to get it to pull the post with the given id and
then pull the collection of comments associated with that post''s id.
def show
@post = Post.find(params[:id])
flash[:post_id]=@post.id
@pages=Page.find(:all)
@posts=Post.find(:all)
@comments=Comment.find(params[@Comment.post_id==@post.id])
end
Then I''m supposed to iterate over the comments in a partial view
_comment.rhtml:
<% @post_comments.each do |comment| %>
<%= render :partial=>"comment", :object => comment
%>
<% end %>
So I''m getting a NoMethod Error in the Show view. Anyone see the
(probably) obvious problem with the code? Also, is post_comments an
automatic variable created from a join table operation?
Ron
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Hi,
@post_comments does not get auto created. But if you have your model
relationships set you could do:
<% @post.comments.each do |comment| %>
<%= render :partial=>"comment", :object => comment %>
<% end %>
In your Post model you''ll need
has_many :comments
That would simplify your code to
def show
@post = Post.find(params[:id])
flash[:post_id]=@post.id
@pages=Page.find(:all)
@posts=Post.find(:all)
end
Henry
On Fri, Mar 7, 2008 at 1:00 PM, Ron
<stecklyenator-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> Hi,
>
> So I''m still trying to work through a tutorial on building a CMS
and
> blog. I have a blog_controller.rb with post and comments. In my show
> action, I''m trying to get it to pull the post with the given id
and
> then pull the collection of comments associated with that post''s
id.
>
> def show
> @post = Post.find(params[:id])
> flash[:post_id]=@post.id
> @pages=Page.find(:all)
> @posts=Post.find(:all)
> @comments=Comment.find(params[@Comment.post_id==@post.id])
> end
>
> Then I''m supposed to iterate over the comments in a partial view
> _comment.rhtml:
>
> <% @post_comments.each do |comment| %>
> <%= render :partial=>"comment", :object => comment
%>
> <% end %>
>
> So I''m getting a NoMethod Error in the Show view. Anyone see the
> (probably) obvious problem with the code? Also, is post_comments an
> automatic variable created from a join table operation?
>
> Ron
> >
>
--
Henry
http://www.henrywagner.org/
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
On 7 Mar 2008, at 21:00, Ron wrote:> > Hi, > > So I''m still trying to work through a tutorial on building a CMS and > blog. I have a blog_controller.rb with post and comments. In my show > action, I''m trying to get it to pull the post with the given id and > then pull the collection of comments associated with that post''s id. > > def show > @post = Post.find(params[:id]) > flash[:post_id]=@post.id > @pages=Page.find(:all) > @posts=Post.find(:all) > @comments=Comment.find(params[@Comment.post_id==@post.id]) > end > > Then I''m supposed to iterate over the comments in a partial view > _comment.rhtml: > > <% @post_comments.each do |comment| %> > <%= render :partial=>"comment", :object => comment %> > <% end %>@post_comments isn''t automatically created, which is probably why you''re getting a no method error also, i doubt @comments=Comment.find(params[@Comment.post_id==@post.id]) does what you actually want it to. Fred> > > So I''m getting a NoMethod Error in the Show view. Anyone see the > (probably) obvious problem with the code? Also, is post_comments an > automatic variable created from a join table operation? > > Ron > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Henry Wagner wrote:> has_many :comments > > That would simplify your code to > > def show > @post = Post.find(params[:id])Also, eager load with Post.find(params[:id], :include => :comments> flash[:post_id]=@post.id > @pages=Page.find(:all) > @posts=Post.find(:all)What role do these play? If you want to show a particular post, then it is unlikely you would need to load them all (@posts) and if the pages are related to the post, then you can eager load them in the same find as the post: @post = Post.find(params[:id], :include => [:comments, :pages]) Then the comments are (as Henry mentioned) @post.comments and the pages are @post.pages -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi, I''m doing similar stuff with posts, comments, users and so. This tutorial helped me a LOT: http://www.akitaonrails.com/2007/12/12/rolling-with-rails-2-0-the-first-full-tutorial It also made me reimplement all I had basically but really cleaned up my way of approaching the work. Cheers -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---