【问题标题】:After getting a post with a collection of comments on the same page, how do I add a new comment in RoR?在同一页面上获得包含评论集合的帖子后,如何在 RoR 中添加新评论?
【发布时间】:2019-03-19 17:45:03
【问题描述】:

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

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

这里是 cmets 控制器的顶部:

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页面,显示了帖子和cmets表的列表:

<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/cmets/show.html.erb 页面来放置评论表单吗?

感谢您的帮助。

【问题讨论】:

    标签: ruby-on-rails model-view-controller


    【解决方案1】:

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

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

    new_comment_path, comment_path, comments_path, etc.
    

    是的,@post 包含 ID。

    我是否需要一个views/cmets/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 应用程序。

    【讨论】:

    • 在 cmets 控制器中将 new 定义为 comment = Comment.new,这允许我从表单的部分填写表单,然后以某种方式进入以 post = Post 开头的创建操作.find(params[:id]),然后是 comment = post.cmets.create(comment_params)。但是,它无法找到没有 id 的帖子。如何获取可用于创建操作的帖子 ID,以便可以将评论添加到帖子集合中? (在适当的地方添加 at 符号)
    • 您可能想查看this article。本质上,has_many 关系(我假设 Post has_many Comments)允许您嵌套表单字段。或者,有时我会在我的表单中放置一个隐藏的输入字段,其中 @post.id 已经设置为名为 :post_id 的字段的值。当用户提交时,我可以从参数中获取它。但我尽量遵循 Rails 约定。通常,当您从表单中手动获取和设置特殊参数时,您可能会错过更好的 Rails 约定。
    • 非常感谢您的帮助。我看了这篇文章,但看不到如何将其应用于我的情况。我发现如果我在'at'post = Post.find('2')中硬连线,即第二个帖子的ID,评论会正确添加到帖子中。正如你所说,我有belongs_to 和has_many 语句。我仍然无法将帖子的 id 放入 cmets_controller 以便我可以找到选定的帖子。我尝试了各种表格上的字段,但仍然没有找到。对放置什么的根本误解。
    • 好的,我正在移动设备上执行此操作,您肯定在苦苦挣扎。只需放置一个名为 post_id 的隐藏字段并在其中设置 post.id 的值。然后位提交并查看您方法中的参数。你看到 post_id 了吗?你应该或者你没有正确地做到这一点。
    • 感谢您的耐心等待。在帖子的显示视图中,我有 'cmets', :action => 'new', :id =>(at)post.id %> 。在部分评论中我有:
      "#@id" %>
      尝试显示传入的 id 并将其作为 post_id 传递。这在控制台中显示为: Started GET "/cmets/new?id=1" for 127.0.0.1 Processing by CommentsController#new as HTML 参数:{"id"=>"1"} 然后 cmets/new.html.erb & _形式。字段显示空白
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 2019-06-13
    相关资源
    最近更新 更多