Ruby on rails 4 Rails 4-所有嵌套的多态注释都显示在每个页面上,而不是单个页面上
我正在学习Rails并尝试创建一个带有嵌套多态注释的站点 我一直在关注本教程以及一些RailsCasts(262棵有祖先的树和154棵多态关联树) 我已经成功地创建了嵌套的多态注释(当我为每个注释检查rails控制台时)。然而,我遇到的问题是输出评论。它们都出现在每个资源的展示页面上,而不是相关的展示页面上。换句话说,所有的评论都显示在/videos/1和videos/2以及lessons/1和lessons/3上(每页都显示相同的评论) 我怀疑我的问题出在comments_helper.rb或/lib/commentable.rb文件中,但是我没有调试这个问题 路线 config/routes.rbRuby on rails 4 Rails 4-所有嵌套的多态注释都显示在每个页面上,而不是单个页面上,ruby-on-rails-4,nested,comments,polymorphic-associations,ancestry,Ruby On Rails 4,Nested,Comments,Polymorphic Associations,Ancestry,我正在学习Rails并尝试创建一个带有嵌套多态注释的站点 我一直在关注本教程以及一些RailsCasts(262棵有祖先的树和154棵多态关联树) 我已经成功地创建了嵌套的多态注释(当我为每个注释检查rails控制台时)。然而,我遇到的问题是输出评论。它们都出现在每个资源的展示页面上,而不是相关的展示页面上。换句话说,所有的评论都显示在/videos/1和videos/2以及lessons/1和lessons/3上(每页都显示相同的评论) 我怀疑我的问题出在comments_helper.rb或
devise_for :users
resources :videos do
resources :comments
end
resources :lessons do
resources :comments
end
控制器
app/controllers/comments\u 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
class LessonsController < ApplicationController
include Commentable
...
end
class VideosController < ApplicationController
include Commentable
...
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
模式
意见
课程和视图show.html.erb
<div class="tab-pane" id="comments">
<%= nested_comments @comments %>
<%= render "comments/form" %>
</div>
<div class="tab-pane" id="comments">
<%= nested_comments @lesson.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 %>
4 %>
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>
评论)%>
任何反馈或想法都将不胜感激。谢谢;) 找到了一个解决方案,但是现在子注释出现了两次 show.html.erb
<div class="tab-pane" id="comments">
<%= nested_comments @comments %>
<%= render "comments/form" %>
</div>
<div class="tab-pane" id="comments">
<%= nested_comments @lesson.comments %>
<%= render "comments/form" %>
</div>
<div class="tab-pane" id="comments">
<%= nested_comments @comments %>
<%= render "comments/form" %>
</div>
<%= 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 %>
<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>
<div class="tab-pane" id="comments">
<%= nested_comments @lesson.comments %>
<%= render "comments/form" %>
</div>