首页 > 解决方案 > 在同一页面上获得包含评论集合的帖子后,如何在 RoR 中添加新评论?

问题描述

在昨天从 Ursus 了解如何获取给定帖子的评论列表后,我能够修改 post/show.html.erb 页面以显示帖子信息和评论列表(仍然为空)在该页面上发布。但是,我不知道如何将“添加评论”链接添加到该页面,该链接会显示评论表单并将帖子的 ID 设置到评论帖子 ID 字段中。我使用的示例说要摆脱整个 Views/comments 目录,但这让我没有显示页面来输入 Comment 数据。下面是后控制器的顶部:

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    @post = Post.find(params[:id])
    @comments = @post.comments
  end

  # GET /posts/new
  def new
    @post = Post.new
  end

  # GET /posts/1/edit
  def edit
  end

这是评论控制器的顶部:

class CommentsController < ApplicationController


  def show
    @comment = Comment.find(params[:id])
  end

  # GET /comments/new
  def new
    @comment = Comment.new
  end

  # POST /comments
  # POST /comments.json
  def create
    @post = Post.find(params[:id])
    @comment = @post.comments.create(comment_params)

    respond_to do |format|
      if @comment.save
        format.html { redirect_to @post, notice: 'Comment was successfully created.' }
        format.json { render :show, status: :created, location: @comment }
      else
        format.html { render :new }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

这里是 posts/show.html.erb 页面,显示帖子和评论表列表:

<p id="notice"><%= notice %></p>
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">

<p>
  <strong>ID:</strong>
  <%= @post.id %>
</p>

<p>
  <strong>Title:</strong>
  <%= @post.title %>
</p>

<p>
  <strong>Body:</strong>
  <%= @post.body %>
</p>


<hr/>
<h1>Comments</h1>

<table id="posts-table">
  <thead>
    <tr>
      <th>Name</th>
      <th>Body</th>
    </tr>
  </thead>

  <tbody>
    <% @comments.each do |comment| %>
      <tr >
        <td><%= comment.name %></td>
        <td><%= comment.body %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>
<hr/>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Add Comment', new_comment_path(@post) %> |
<%= link_to 'Back', posts_path %>

<script>
  $(function(){
    $("#posts-table").dataTable();
  });
</script>

我对几个项目感到困惑:

  1. new_comment_path 来自哪里,@post 是否包含创建评论并将其链接到帖子所需的 id?
  2. 我需要一个views/comments/show.html.erb 页面来放置评论表单吗?

感谢帮助。

标签: ruby-on-railsmodel-view-controller

解决方案


new_comment_path 来自哪里,@post 是否包含创建评论并将其链接到帖子所需的 id?

它来自/config/routes.rb。Resources是定义典型的关键字

new_comment_path, comment_path, comments_path, etc.

是的,@post 包含 ID。

我需要一个views/comments/show.html.erb 页面来放置评论表单吗?

如果您愿意,您可以,您没有义务在 Rails 应用程序中显示任何内容或添加任何表单。许多 Rails 应用程序都是这样配置的:views/comments/_form.html.erb并且会在任何需要的地方将表单呈现为部分。

您可以将表格放置在任何地方。不要忘记,归根结底,您只是在提供 HTML。Rails看起来很特别,因为它有一些关于控制器动作到视图映射的预设配置。但是你可以做任何你想做的事情。

例如。

# inside ANY controller
def show
  @comment = Comment.first
  render 'comments/show.html.erb'
end

这是完全有效的,尽管这是一个非常糟糕的做法。但是在 Rails 中,您不会被它的任何配置所束缚。它们的存在只是为了更快、更轻松地编写 Web 应用程序。


推荐阅读