your are doing a polimorphic assotiacion on the same model and another model , thats not needed, you only need a self referential association and a has many, your main problem is that you are using a polymorphic associations where there was no need to.
you comment table should have t.integer :article_id t.integer :parent_id drop the polymorphic association if the comments will only belong to articles and install the acts_as_three gem, then you'll be done with it. if you want to apply comment to something else besides articles then use the polymorphic association bad that is not need for the comments they just need to refer to themself. On Sat, Sep 11, 2010 at 12:07 AM, Kelp Kelp <[email protected]> wrote: > I've been having problems making threaded comments for the last few > days. Mainly with creating children and displaying the children. > Currently, I have comments shown at the bottom of my articles show view. > The top level comments work, but I do not really know how to implement a > way for users to "reply" to the top level comments and take them to a > form (the comment 'new' view) to create a new comment. I want the > controller to determine if the user is creating the new comment in an > article or in a comment and create the new comment as a child if its > parent is a comment. I've been facing multiple problems, maybe the > recursion messes up and I get an infinite loop. > > Here is the articles show controller: > def show > @article = Article.find(params[:id]) <====== you are > query the DB twice change this to > @commentable = Article.find(params[:id]) @article > =...@commentable=article.find(params[:id]) > @comments = @commentable.comments > # @comments = @commentable.comments.paginate(:page => params[:page]) > @comment = Comment.new > @title = @article.title > end > > Here is my comments controller: > def show > @commentable = find_commentable > @comment = @commentable.comments.find(params[:id]) > # @comments = @commentable.comments.paginate(:page => params[:page]) > end > > def new > @commentable = find_commentable > @comment = Comment.new <==== this here makes no > sense it should be @commentable.build > end > > def create > @commentable = find_commentable > if @comment.commentable_type == "Comment" <===== you > are doing polimorphic to the same model?? > @comment = @commentable.children.create(param[:comment]) > else > @comment = @commentable.comments.build(params[:comment]) > end > @comment.user_id = current_user.id <============== > change to: @comment.user = current_user > if @comment.save > flash[:success] = "Comment saved." > redirect_to @commentable <============== > change to: redirect_to :back > else > flash[:error] = "Error in creating comment." > # @comments = @commentable.comments.paginate(:page => > params[:page]) > render 'new' > end > end > > Here is the comment creation form: > <%= form_for([...@commentable, @comment]) do |f| %> > <%#= render 'shared/error_messages', :object => f.object %> > <========= > change to: :f => f > <div class="field"> > <%= f.label :title %><br /> > <%= f.text_field :title %> > </div> > <div class="field"> > <%= f.label :content %><br /> > <%= f.text_area :content %> > </div> > <div class="actions"> > <%= f.submit "Post Comment" %> > </div> > <% end %> > > Here is the loop that goes through all of the existing comments: > <% unless @comments.nil? || @comments.empty? %> > <%= render :partial => 'comments/comment', :collection => > @comments <=== change to: render @comments > %> > <%#= will_paginate @comments %> > <% end %> > > Here is what each individual comment looks like: > <div id="comment <%= comment.id %>"> > <%= comment.title %> <=== > you are not scaping html > | <%= link_to "Permalink", polymorphic_path([...@commentable, comment]), > :action => :show %> <======= no need for this > | <%= link_to "Reply", polymorphic_path([...@commentable, @comment]),<=== > pass [:new, @commentable, comment] instead > :action => :new %> > | <%= link_to "Edit Comment", polymorphic_path([...@commentable, > comment]), :action => :edit %> <=== pass [:edit, @commentable, > comment] instead > | <%= link_to 'Delete Comment', [...@commentable, comment]], :confirm => > "Are you sure?", :method => :delete %><br /> > <%= comment.content %><br /> > <%= comment.user.name %><br /><br /> > <%= render :partial => 'comments/comment', :collection => > @comment.children %> <=== use acts_as_three gem > </div> > > When I click reply, I get taken to: > http://localhost:3000/articles/299/comments and not > http://localhost:3000/articles/299/comments/new. I went into rails > console and gave one of my comments a comment child, and the recursive > nature of the threaded comments gave me recursion that crashed my site. > -- > 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 [email protected]. > To unsubscribe from this group, send email to > [email protected]<rubyonrails-talk%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- 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 [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

