我正在尝试构建一个简单的博客,其中一篇帖子有很多评论,评论显示在帖子#show page上。
我有两个我无法弄清楚的错误:
routes.rb中:
resources :posts do
resources :comments
end
应用/模型/ post.rb
class Post < ActiveRecord::Base
has_many :comments, dependent: :destroy
end
应用/模型/ comment.rb
class Comment < ActiveRecord::Base
validates :name, presence: true
validates :content, presence: true
belongs_to :post
end
应用/控制器/ comments_controller.rb
class CommentsController < ApplicationController
before_action :set_post
def create
@comment = @post.comments.build(comment_params)
if @comment.save
flash[:success] = "Comment saved!"
redirect_to post_path(@post)
else
flash[:alert] = "Something went wrong!"
render root_path # I suspect here is an error?
end
end
def destroy
@comment = @post.comments.find(params[:id])
@comment.destroy
flash[:success] = "Comment deleted"
redirect_to post_path(@post)
end
private
def comment_params
params.require(:comment).permit(:name, :email, :content)
end
def set_post
@post = Post.find(params[:post_id])
end
end
应用/视图/帖/ show.html.erb
每当我创建新帖子时,都会出现“删除”链接的问题。这个“删除”链接属于一个空的并且自动奇怪地创建的注释?
<% unless @post.comments.empty? %> # I suspect here is an error?
<% @post.comments.each do |comment| %>
<p><%= comment.name %></p>
<p><%= comment.content %></p>
<p><%= link_to "Delete", [comment.post, comment],
method: :delete,
data: { confirm: "Are you sure?" } %></p>
<% end %>
<% end %>
应用/视图/帖/ new.html.erb
<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= @post.errors.count %> Fehler:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="image-box">
<p><%= f.file_field :image%></p>
</div>
<div class="title-box">
<p><%= f.label :title, "Titel" %></p>
<p><%= f.text_field :title, class: "title-field"%></p>
</div>
<div class="content-box">
<p><%= f.label :content, "Inhalt" %></p>
<p><%= f.text_area :content, class: "content-field"%></p>
</div>
<p><%= f.submit %></p>
<% end %>
答案 0 :(得分:1)
我没有使用@post
作为控制器中的主要对象来创建评论,而是实际上这样做:
@comment = Comment.new(comment_params)
@comment.post = @post
if @comment.save
[...]
答案 1 :(得分:1)
实际上你并没有将post_id
传递给创建方法,这就是为什么它没有保存对帖子的评论。
将post_id添加到post#new
表单中作为隐藏字段,如:
<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= @post.errors.count %> Fehler:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="image-box">
<p><%= f.file_field :image%></p>
</div>
<div class="title-box">
<p><%= f.label :title, "Titel" %></p>
<p><%= f.text_field :title, class: "title-field"%></p>
<%= hidden_field_tag :post_id, @post.id %>
</div>
<div class="content-box">
<p><%= f.label :content, "Inhalt" %></p>
<p><%= f.text_area :content, class: "content-field"%></p>
</div>
<p><%= f.submit %></p>
<% end %>
这有望解决您的问题。
或者您可以在CommentsController
强参数中允许post_id并发送post_id
以及评论新表单以创建方法&amp;创造将像:
def create
@comment = Comment.new(comment_params)
if @comment.save
flash[:success] = "Comment saved!"
redirect_to post_path(@post)
else
flash[:alert] = "Something went wrong!"
render root_path
end
end
def comment_params
params.require(:comment).permit(:name, :email, :content, :post_id)
end
答案 2 :(得分:1)
我在这里看到的问题是评论没有保存在数据库中,这就是为什么它没有id,这就是你得到这个html的原因:
<a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/posts/51/comments">delete</a>
缺少ID。
如果我是你,我会在渲染视图时检查评论是否存在,这样您只会向用户显示现有评论:
<% if comment.persisted? %>
[...]