我正在创建一个模型来为帖子添加评论。当我运行服务器时没有错误消息,但是当我填充文本框后单击“提交”按钮时,什么也没有发生。它不创建评论(并且没有错误消息)。现在我被卡住了。可以看看我的程序,并告诉我是否编码错了? 非常感谢您的支持Ruby on rails对帖子的评论
comments_controller.rb
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment= @post.comments.create(params[:comment].permit(:comment))
if @comment.save
redirect_to post_path(@post)
else
render 'new'
end
end
end
的意见/ _form.html.erb
<%= simple_form_for [@post, @post.comments.build] do |f| %>
<%= f.input :comment %>
<%= f.submit %>
<% end %>
帖子/ show.html .erb
<h1><%= @post.title %></h1>
<p><%= @post.content %></p>
<h3> Reply to thread </h3>
<%= render "comments/form" %>
感谢您的反馈。即使我在代码中渲染评论,它仍然不起作用。 – Antoine
@Antoine请用'create!'替换'create'并告诉我们是否有错误。 –