После вчерашнего узнавания от Ursus о том, как получить список комментариев к данному сообщению, я смог изменить страницу post / show.html.erb, чтобы отображать информацию сообщения и список (пока еще пустой)комментарии для этого поста на этой странице.Однако я не знаю, как добавить ссылку «Добавить комментарий» на эту страницу, чтобы вызвать форму комментария и задать идентификатор сообщения в поле идентификатора комментария.Пример, который я использовал, сказал, чтобы избавиться от всего каталога Views / comments, но это не дает мне никакой страницы для ввода данных комментариев.Ниже находится верхняя часть контроллера сообщений:
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>
Я запутался в нескольких пунктах:
- откуда берется new_comment_path и содержит ли @post элементid нужен для создания комментария и связывания его с постом?
- Нужна ли мне страница views / comments / show.html.erb для размещения формы комментария?
Ценю помощь.