我正在学习 Rails 并尝试创建一个带有嵌套多态注释的站点。
我一直在关注本教程http://www.tweetegy.com/2013/04/create-nested-comments-in-rails-using-ancestry-gem/以及一些 RailsCasts(262-trees-with-ancestry 和154-多态关联)。
我已经成功地创建了嵌套的多态评论(当我检查 rails 控制台的每个评论时)。但是,我遇到的问题是输出评论。它们都显示在每个资源的显示页面上,而不是相关的显示页面上。换句话说,所有评论都显示在/videos/1 和videos/2 和课程/1 和课程/3 上(每个页面上都显示相同的评论)。
我怀疑我的问题出在 comments_helper.rb 或 /lib/commentable.rb 文件中,但是我没有运气调试问题。
路线
配置/路由.rb
devise_for :users
resources :videos do
resources :comments
end
resources :lessons do
resources :comments
end
控制器
应用程序/控制器/comments_controller.rb
class CommentsController < ApplicationController
def new
@parent_id = params.delete(:parent_id)
@commentable = find_commentable
@comment = Comment.new( :parent_id => @parent_id,
:commentable_id => @commentable.id,
:commentable_type => @commentable.class.to_s)
end
def create
@commentable = find_commentable
@comment = @commentable.comments.build(comment_params)
@comment.user_id = current_user.id
if @comment.save
flash[:notice] = "Successfully created comment."
redirect_to @commentable
else
flash[:error] = "Error adding comment."
end
end
private
def find_commentable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
def comment_params
params.require(:comment).permit(:body, :parent_id, :commentable_id, :commentable_type)
end
end
应用程序/控制器/lessons_controller.rb
class LessonsController < ApplicationController
include Commentable
...
end
应用程序/控制器/videos_controller.rb
class VideosController < ApplicationController
include Commentable
...
end
楷模
应用程序/模型/comment.rb
class Comment < ActiveRecord::Base
has_ancestry
belongs_to :user
belongs_to :commentable, polymorphic: true
end
应用程序/模型/课程.rb
class Lesson < ActiveRecord::Base
has_many :comments, :as => :commentable, :dependent => :destroy
end
应用程序/模型/video.rb
class Video < ActiveRecord::Base
has_many :comments, :as => :commentable, :dependent => :destroy
end
应用程序/模型/user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :comments, :as => :commentable, :dependent => :destroy
end
帮手
应用程序/帮助者/comments_helper.rb
module CommentsHelper
def nested_comments(comments)
comments.map do |comment, sub_comments|
content_tag(:div, render(comment), :class => "media media-nested")
end.join.html_safe
end
end
库
/lib/commentable.rb
require 'active_support/concern'
module Commentable
extend ActiveSupport::Concern
included do
before_filter :comments, :only => [:show]
end
def comments
@commentable = find_commentable
@comments = @commentable.comments.arrange(:order => :created_at)
@comment = Comment.new
end
private
def find_commentable
return params[:controller].singularize.classify.constantize.find(params[:id])
end
end
架构
create_table "comments", force: true do |t|
t.text "body"
t.integer "user_id"
t.integer "commentable_id"
t.string "commentable_type"
t.datetime "created_at"
t.datetime "updated_at"
t.string "ancestry"
end
add_index "comments", ["ancestry"], name: "index_comments_on_ancestry"
意见
课程与观点 show.html.erb
<div class="tab-pane" id="comments">
<%= nested_comments @comments %>
<%= render "comments/form" %>
</div>
app/views/comments/_form.html.erb
<%= form_for [@commentable, @comment] do |f| %>
<%= f.hidden_field :parent_id %>
<p>
<%= f.label :body, "New comment" %>
</p>
<%= f.text_area :body, :rows => 4 %>
<p>
<%= f.submit "Post Comment" %>
</p>
<% end %>
app/views/comments/_comment.html.erb
<div class="media-body">
<h4 class="media-heading"><%= comment.user.username %></h4>
<i>
<%= comment.created_at.strftime('%b %d, %Y at %H:%M') %>
</i>
<p>
<%= simple_format comment.body %>
</p>
<div class="actions">
<%= link_to "Reply", new_polymorphic_path([@commentable, Comment.new], :parent_id => comment) %>
</div>
<%= nested_comments comment.children %>
</div>
任何反馈或想法将不胜感激。谢谢 ;)