{"id":135,"date":"2006-07-09T14:54:45","date_gmt":"2006-07-09T19:54:45","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/07\/09\/comments-on-acts-as-commentable\/"},"modified":"2006-07-09T14:54:45","modified_gmt":"2006-07-09T19:54:45","slug":"comments-on-acts-as-commentable","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2006\/07\/09\/comments-on-acts-as-commentable\/","title":{"rendered":"Comments on Acts As Commentable"},"content":{"rendered":"<p>I have received a bit positive <a href=\"http:\/\/www.juixe.com\/contact.html\">feeback<\/a> rearding the <a href=\"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/06\/18\/acts-as-commentable-plugin\/\">acts_as_commentable<\/a> rails plugin.  One suggestion asked for me to provide more sample code, especially in regards to the view and controller.  To fulfill that request lets imagine that we are developing an application where different type of models act as commentable.  We can start by creating a <b>_comment_form.rhtml<\/b> partial view that can be used by other views.  The comment form should have the following fields:<\/p>\n<pre>\n&lt;% start_form_tag :action =&gt; 'add_comment' %&gt;\n  &lt;%= text_field 'comment', 'title' %&gt;\n  &lt;%= text_field 'comment', 'comment' %&gt;\n\n  &lt;%= submit_tag 'Add Comment' %&gt;\n\n  &lt;%= hidden_field 'commentable', 'commentable', :value =&gt; @model.type %&gt;\n  &lt;%= hidden_field 'commentable', 'commentable_id', :value =&gt; @model.id %&gt;\n&lt;% end_form_tag %&gt;\n<\/pre>\n<p>The @model variable holds an intance to the record you want to add a comment to.  In this example, the <b>add_comment<\/b> action will be generic enough that it will be able to add comments to any ActiveRecord in the application.  Because the add_comment action can add comments to say either Tasks or Posts you need to pass the model type to the action via a hidden field.<\/p>\n<p>In the controller you can define the add_comment action like this:<\/p>\n<pre>\ndef add_comment\n  commentable_type = params[:commentable][:commentable]\n  commentable_id = params[:commentable][:commentable_id]\n  # Get the object that you want to comment\n  commentable = Comment.find_commentable(commentable_type, commentable_id)\n\n  # Create a comment with the user submitted content\n  comment = Comment.new(params[:comment])\n  # Assign this comment to the logged in user\n  comment.user_id = session[:user].id\n\n  # Add the comment\n  commentable.comments &lt;&lt; comment\n\n  redirect_to :action =&gt; commentable_type.downcase,\n    :id =&gt; commentable_id\nend\n<\/pre>\n<p>In the sample code for the action there is an assumtion that only logged in user can add comments and that there is a action named after the commentable active record type.<\/p>\n<p>Technorati Tags: <a href=\"http:\/\/technorati.com\/tag\/ruby\" rel=\"tag\">ruby<\/a>, <a href=\"http:\/\/technorati.com\/tag\/rails\" rel=\"tag\"> rails<\/a>, <a href=\"http:\/\/technorati.com\/tag\/ruby+on+rails\" rel=\"tag\"> ruby on rails<\/a>, <a href=\"http:\/\/technorati.com\/tag\/rails+plugin\" rel=\"tag\"> rails plugin<\/a>, <a href=\"http:\/\/technorati.com\/tag\/rails+plugins\" rel=\"tag\"> rails plugins<\/a>, <a href=\"http:\/\/technorati.com\/tag\/acts+as+commentable\" rel=\"tag\"> acts as commentable<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have received a bit positive feeback rearding the acts_as_commentable rails plugin. One suggestion asked for me to provide more sample code, especially in regards to the view and controller. To fulfill that request lets imagine that we are developing an application where different type of models act as commentable. We can start by creating [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[22,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-2b","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/135"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=135"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}