On Fri, Sep 17, 2010 at 7:57 PM, Kelp Kelp <[email protected]> wrote:
> radhames brito wrote: > > oh , i forgot you are doing polymorphic associations sorry, > > > > > > redirect_to polymorphic_path(@commentable) > > > > > > remember to catch @commentable with the find_commentable method > > > > you can find it iterating the params hash or user a context_Type in the > > route , i like that last one more. > > Do you know if there is anyway for my find_commentable method to treat a > comment the same way as an article? > > So, when a user tries to reply to a comment, the URL looks like this: > http://localhost:3000/comments/new?in_reply_to=56 > > for this you will need to create another controller otherwise it will get messy and you will have to catch the owner of the first comment. One thing you should understand is that you keep trying to treat comments as if they were not associated and as if they were, let me explain. comments belong to commentable, and you created this route articles :has_many :comments this route is nesting commentes like this /articles/1/comments this is to help you get the parent of the comment, the params hash will catch that id after the articles and create a key called articles_id, take a look at the find_commentable function def find_commentable params.each do |name, value| if name =~ /(.+)_id$/ return $1.classify.constantize.find(value) end end nilend For each value in the params hash,( note that the params hash has this format, :id=>"1", this is , name=>value), it uses a regular expression to see if the name ends with _id, hoping that only the commentable object will end like this i then takes the first part of the name( thats what $1 does if there are more (.+) in the regular expresion you refer to them in order, $2 , $3 etc), in case of article_id it will take article , then with constantize it becomes Article and a find is perform passing as an id the value corresponding to article_id, so it return the instance that has those comments as childs. now lets say you try to do this without articles :has_many :comments you have to provide the parent object somehow , if you access http://localhost:3000/comments/new?in_reply_to=56 who does that object belongs to really when you access /articles/1/comments, active record will see that the comments created that way have no commetable_type and no commetable_id, those are the 2 fields AR uses for the polymorphic associations. What you are trying yo do is easy, but is not the rigth way , you have to have comments belong to commentable and to themself then on the commetable show iterate the comments that have no parent . inside the iteration iterate for their children. > When a user replies to an article (or profile), it looks like this: > http://localhost:3000/articles/304 > > The find_commentable way only works for the second method URL, since it > cannot find a commentable in the first one. > > Sorry, this is my first Rails application, so my code is very cluttered, > and I don't think I thought everything through the first time. > > Here's my comments controller: > http://gist.github.com/585167 > -- > 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.

