是否有一种干净的方法在Rails应用程序中实现acts_as_commentable?

时间:2010-09-18 00:21:41

标签: ruby-on-rails acts-as-commentable

README没有显示如何处理控制器和查看设置此插件的方面。我一直在搜索几个小时,找不到任何显示如何使用此插件的内容。

3 个答案:

答案 0 :(得分:18)

经过更多的搜索,我放弃了寻找教程并想出了这个。如果有人能指出更好/更清洁的方式来做到这一点,请告诉我。否则,这就是我现在正在使用的,以防这将使其他任何人受益。

首先,使用script/plugin install http://github.com/jackdempsey/acts_as_commentable.git -r 2.x

安装插件

然后,使用script/generate comment生成评论模型和迁移,并使用rake db:migrate

迁移数据库

棘手的一点是以多态方式在其他资源下嵌套注释。这是我做的:

# In config/routes.rb
map.resources :comments, :path_prefix => '/:commentable_type/:commentable_id'


# In app/controllers/comments_controller.rb
before_filter :load_commentable
def create
  @comment = @commentable.comments.build(params[:comment])
  @comment.user = current_user
  respond_to do |format|
    if @comment.save
      format.html { redirect_to @commentable }
    else
      format.html { render :action => 'new' }
    end
  end
end

protected
def load_commentable
  @commentable = params[:commentable_type].camelize.constantize.find(params[:commentable_id])
end


# In app/views/comments/_form.html.erb
<%= form_for(:comment, :url => comments_path(commentable.class.to_s.underscore, commentable.id)) do |f| %>


# In app/views/model_that_allows_comments/show.html.erb
<%= render :partial => 'comments/form', :locals => {:commentable => @model_that_allows_comments} %>

我认为这清楚地显示了相关部分,以便了解正在发生的事情。它可以将acts_as_commentable添加到任何模型中。您只需在呈现注释表单时传入locals哈希中的可注释对象,并且相同的注释控制器/视图代码应该有效。

答案 1 :(得分:7)

acts_as_commentable只会向您展示一个评论模型,并负责该模型与您的可评论模型之间的管道。它不会为您提供任何控制器或视图。您有责任决定如何实施应用程序的这一部分。

但这很简单。例如......

# in routes.rb
map.resources :posts, :has_many => :comments

# in your comments controller...
class CommentsController < ApplicationController
  before_filter :get_post

  def get_post
    @post = Post.find(params[:post_id])
  end

  def index
    @comments = @post.comments.all # or sorted by date, or paginated, etc.
  end
end

# In your haml view...
%h1== Comments for #{@post.title}:
%ul
  - comments.each do |comment|
    %h3= comment.title
    %p= comment.comment

现在转到/posts/1/comments,您会看到特定帖子的评论。

答案 2 :(得分:2)

我认为向任何模型添加注释的最佳方法是在ApplicationController.rb文件中创建一个名为comment的方法,如下所示。

def comment 
  # Extracts the name of the class
  klass = self.class.to_s[/\A(\w+)sController\Z/,1] 
  # Evaluates the class and gets the current object of that class
  @comentable_class = eval "#{klass}.find(params[:id])"
  # Creates a comment using the data of the form
  comment = Comment.new(params[:comment])
  # Adds the comment to that instance of the class
  @comentable_class.add_comment(comment)

  flash[:notice] = "Your comment has been added!"
  redirect_to :action => "show", :id => params[:id] 
end    

然后只需创建一些部分_comment.rb即可在任何你想要的模型中使用它

<%= form_tag :action => "comment", :id => Your_model_goes_here %>
  <p><label for="comment_title">Title</label><br/>
  <%= text_field 'comment', 'title' %></p>
  <%= text_area "comment", "comment", :rows => 5, :cols => 50 %> <br />
  <%= submit_tag "Comment!.." %>
</form>

我希望它对某人有用......