我想將評論表單添加到博客文章中,並且也顯示歷史註釋。我創建了兩個部分。我可以在posts/show視圖中看到評論表單,並且我可以留下評論,並將評論添加到數據庫中,但這些評論是 不顯示。無法獲取歷史註釋以在視圖文件中顯示
我如何獲得這些歷史評論?
這裏是我的相關文件/摘錄:
應用程序/視圖/職位/ show.html.erb
<%= render partial: '/comments/comment', locals: { comments: @comments} %>
<%= render partial: '/comments/form', locals: { comment: @comment } %>
應用程序/意見/評論/ _comment.html.erb
<% comments.each do |comment| %>
<%= comment.body + " " + comment.user.name %>
<% end %>
app/views/comments/_form.html.erb
<%= form_for [@post, comment] do |f| %>
<%= f.label :body %>
<%= f.text_area :body %>
<%= f.submit "Save" %>
<% end %>
應用程序/控制器/ posts_controller.rb
def show
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:id])
@comments = @post.comments
@comment = Comment.new
end
應用程序/控制器/ comments_controller.rb
def create
@post = Post.find(params[:post_id])
@topic = @post.topic
@comment = current_user.comments.new(comment_params)
@comment.post = @post
if @comment.save
redirect_to [@topic, @post], notice: "Comment was saved successfully."
else
flash[:error] = "Error creating comment. Please try again."
render :new
end
end
感謝。