我正在創建一個模型來爲帖子添加評論。當我運行服務器時沒有錯誤消息,但是當我填充文本框後單擊「提交」按鈕時,什麼也沒有發生。它不創建評論(並且沒有錯誤消息)。現在我被卡住了。可以看看我的程序,並告訴我是否編碼錯了? 非常感謝您的支持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'並告訴我們是否有錯誤。 –