【发布时间】:2014-11-04 15:27:38
【问题描述】:
我正在尝试将 cmets 表单添加到博客文章中,并显示历史 cmets。我创建了两个部分。我可以在posts/show视图中看到评论表单,我可以离开cmets,并且将cmets添加到数据库中,但是那些cmets是 不显示。
如何才能显示这些历史 cmets?
这是我的相关文件/摘录:
app/views/posts/show.html.erb
<%= render partial: '/comments/comment', locals: { comments: @comments} %>
<%= render partial: '/comments/form', locals: { comment: @comment } %>
app/views/cmets/_comment.html.erb
<% comments.each do |comment| %>
<%= comment.body + " " + comment.user.name %>
<% end %>
app/views/cmets/_form.html.erb
<%= form_for [@post, comment] do |f| %>
<%= f.label :body %>
<%= f.text_area :body %>
<%= f.submit "Save" %>
<% end %>
app/controllers/posts_controller.rb
def show
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:id])
@comments = @post.comments
@comment = Comment.new
end
app/controllers/cmets_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
谢谢。
【问题讨论】:
标签: ruby-on-rails