线程评论对我不起作用

时间:2010-09-14 21:08:48

标签: ruby-on-rails

我的网站能够让用户成功发布顶级评论,但我对评论儿童有很多麻烦。我正在使用多态关联,因此注释模型可以应用于文章,配置文件和图片。在这三种情况中,评论都会将模型视为“可评论”。

我想在每个评论旁边创建一个回复链接,该回复链接将该特定评论的“id”传递到评论“新”视图页面。然后我希望注释“create”方法使用该id,@ top_level_comment = Comment.find(id),并使用用户在“new”注释视图中指定的参数创建一个新子项,@ top_level_comment.children.create (params [:comment]),但我不知道如何把它放到代码中。

因此,用户将从文章显示页面开始,该页面具有以下控制器:

  def show
    @article = Article.find(params[:id])
    @commentable = Article.find(params[:id])
    @comments = @commentable.comments
    @comment = Comment.new
    @title = @article.title
  end

然后,他将向下滚动到底部的评论,并通过文章显示页面看到有回复链接:

<div id="comment <%= comment.id %>">
  <%= comment.title %>
  | <%= link_to "Permalink", polymorphic_path([@commentable, comment]), :action => :show %>
  | <%= link_to "Reply", polymorphic_path([@commentable, @comment]), :action => :new %>
  | <%= link_to "Edit Comment", polymorphic_path([@commentable, comment]), :action => :edit %>
  | <%= link_to 'Delete Comment', [[@commentable, comment]], :confirm => "Are you sure?", :method => :delete %><br />
  <%= comment.content %><br />
  <%= comment.user.name %><br />
  <%= @comment.children.count %><br /><br />
  <%= render :partial => 'shared/comment', :collection => @comment.children %>

</div>

特别是在遇到问题的时候,这就是这条线:

| &lt;%= link_to“Reply”,polymorphic_path([@ commentable,@ comment]),:action =&gt; :new%&gt;

如何根据此帖子顶部的说明更改此操作以执行我想要执行的操作?多态路径不起作用。我认为它不起作用,因为可评论仅适用于文章,个人资料和图片,但不适用于评论。如何更改路径,以便在传递当前注释的id(comment.id)时转到注释“new”页面?

另外,我添加新评论的表单如下所示:

<%= form_for([@commentable, @comment]) do |f| %>
  <%#= render 'shared/error_messages', :object => f.object %>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit "Post Comment" %>
  </div>
<% end %>

这是路由的样子:

  resources :articles do
    resources :comments
  end

您好,当我按照标记的路由时,我在尝试查看文章时得到了这个:

No route matches {:controller=>"comments", :action=>"create", :article_id=>#<Article id: 300, title: "Filler Title", content: "Sunt sit est incidunt et.", user_id: 6, created_at: "2010-09-08 17:42:10", updated_at: "2010-09-08 17:42:10">}

在太平洋标准时间9月16日3:48添加了以下新信息:

这是允许用户对可评论进行评论的表单,但是当@commentable是评论时它不起作用。

1: <%= form_for([@commentable, @comment]) do |f| %>
2:   <%#= render 'shared/error_messages', :object => f.object %>
3:   <div class="field">
4:     <%= f.label :title %><br />

评论控制器“新”

  def new
    @commentable = find_commentable
    @comment = Comment.new
  end

在回复评论时,它会转到上面,但它不知道@commentable是什么,因此在回复评论时其值为nil。如何使@commentable成为用户按下回复的@comment?以下是允许用户回复评论的链接:

  | <%= link_to "Reply", new_comment_path(comment.children.new) %>

非常感谢。

1 个答案:

答案 0 :(得分:1)

当然,如果你使用注释的多态关系,你应该在注释和注释之间定义一个自引用的多态关系,即。评论有许多评论可评论。

然后这个:

<%= link_to "Reply", [@comment, @comment.comments.new] %>

#routes.rb

map.resources :comments, :articles, :profiles, :pictures do |commentable|
  commentable.resources :comments, :only => [:new, :create]
end
相关问题