Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andre' Jones
26,671 PointsActs_as_commentable with threading gem
Looking at the github page https://github.com/elight/acts_as_commentable_with_threading I can follow the instructions but after i Add a comment to a model instance, for example an Article: I hav no clue where to go from there. Anyone know a good tutorial for this I've looked found nothing and all the stack overflow dicussions just point back to the github page
3 Answers
Jason Seifer
Treehouse Guest TeacherHi Andre' Jones I don't have a link to any specific tutorials but what are you having problems with? You'll need to make a comment form after installing that gem and then hook it up to a controller action. The documentation says to do this:
@article = Article.find(params[:id])
@user_who_commented = @current_user
@comment = Comment.build_from( @article, @user_who_commented.id, "Hey guys this is my comment!" )
You would need to create a comments controller and a new action, where you build a comment based on what you send it. So if you had a list of articles you could link to the new_comment_path(article) and have the following form:
<%= form_for @comment, comments_path(@article) do |f| %>
<%= f.text_area :body %>
<%= submit_tag "Post comment" %>
<% end %>
Then your comments controller would have the code they linked in the documentation in the create action. It's a bit difficult figure out but try creating a few models and controllers to get the idea down and it should make a bit more sense. Also check out the polymorphic lessons in the Ruby tutorials.
Jason Seifer
Treehouse Guest TeacherKeep working through it Andre' Jones! Good luck!
Andre' Jones
26,671 PointsFigured it out. Have another Question but I will make another post for it
Andre' Jones
26,671 PointsAndre' Jones
26,671 PointsComments_controller
class CommentsController < ApplicationControllerdef create @comment_hash = params[:comment] @obj = @comment_hash[:commentable_type].constantize.find(@comment_hash[:commentable_id]) # Not implemented: check to see whether the user has permission to create a comment on this object @comment = Comment.build_from(@obj, current_user, @comment_hash[:body]) if @comment.save render :partial => "comments/comment", :locals => { :comment => @comment }, :layout => false, :status => :created else render :js => "alert('error saving comment');" end end
def destroy @comment = Comment.find(params[:id]) if @comment.destroy render :json => @comment, :status => :ok else render :js => "alert('error deleting comment');" end end end
Comment.rb - I added the attr_accessible to this but im still getting the error cannont mass assign user_id and i read that adding it to the attr_accessible may not be the best option.
class Comment < ActiveRecord::Baseacts_as_nested_set :scope => [:commentable_id, :commentable_type] attr_accessible :commentable, :body
validates :body, :presence => true validates :user, :presence => true
# NOTE: install the acts_as_votable plugin if you # want user to vote on the quality of comments. #acts_as_votable
belongs_to :commentable, :polymorphic => true
# NOTE: Comments belong to a user belongs_to :user
# Helper class method that allows you to build a comment # by passing a commentable object, a user_id, and comment text # example in readme def self.build_from(obj, user_id, comment) new \ :commentable => obj, :body => comment, :user_id => user_id end
#helper method to check if a comment has children def has_children? self.children.any? end
# Helper class method to lookup all comments assigned # to all commentable types for a given user. scope :find_comments_by_user, lambda { |user| where(:user_id => user.id).order('created_at DESC') }
# Helper class method to look up all comments for # commentable class name and commentable id. scope :find_comments_for_commentable, lambda { |commentable_str, commentable_id| where(:commentable_type => commentable_str.to_s, :commentable_id => commentable_id).order('created_at DESC') }
# Helper class method to look up a commentable object # given the commentable class name and id def self.find_commentable(commentable_str, commentable_id) commentable_str.constantize.find(commentable_id) end end
Views Blog/show.html.erb
</div>
My two partials are haml instead of erb is this ok or should I try to convert it to erb
Blogs/_form.html.haml
I will check out those polymorphic lessons too
Andre' Jones
26,671 PointsAndre' Jones
26,671 Pointshaving trouble getting the markdown to display it correctly but im following this tutorial
http://twocentstudios.com/blog/2012/11/15/simple-ajax-comments-with-rails/
Stuck on this error right now
Can't mass-assign protected attributes: user_id - (I read on stackoverflow not to add this to attr_accesible)