您好我想在post#show视图中添加一个destroy动作我想我已经创建了环境但是当我把这个代码放在一个条件时我有一个消息错误。
<% if @user.comment == current_user %>
<% link_to @post_comment_path(post_id: @post.id, id: comment.id), method: :delete, data: { confirm: "Are you sure?" } do %>
<i class="fa fa-trash"></i>
<% end %>
<% end %>
我在post show#view中创建了一个名为_comments.html.erb
的部分内容这里是
<p class="text-center">Poster un commentaire</p>
<%= simple_form_for [post, post.comments.new] do |f| %>
<%= f.error_notification %>
<%= f.input :content, label: "Commentaire"%>
<%= f.submit "Envoyer", class: "btn btn-primary" %>
<% end %>
它呈现<%= render 'comments' %>
在部分之上(在后期节目#view中)我做了类似的迭代
<ul class="list-unstyled">
<% @post.comments.each do |comment| %>
<li>
<p><% comment.content %></p>
<% end %>
</li>
</ul>
但是当我创建新消息时,没有任何内容出现,我不会告诉用户原因。
我提供更多代码详情
post.rb
has_many :comments, dependent: :destroy
comment.rb
belongs_to :user
belongs_to :post
路线是:
resources :posts do
resources :categories
resources :comments
end
评论控制器
class CommentsController < ApplicationController
before_action :set_post
def create
@comment = @post.comments.build(comment_params)
@comment.user_id = current_user.id
if @comment.save
flash[:success] = "You commented the hell out of that post!"
redirect_to :back
else
flash[:alert] = "There is a problem with your comment"
render root_path
end
end
def destroy
@comment = @post.comments.find(params[:id])
@comment.destroy
flash[:success] = "Comment deleted :("
redirect_to root_path
end
private
def set_post
@post = Post.find(params[:post_id])
end
def comment_params
params.require(:comment).permit(:content, :post_id, :user_id)
end
end
非常感谢你的帮助。
答案 0 :(得分:1)
你的link_to erb代码需要一个等号来实际显示
<%= link_to post_comment_path(post_id: @post.id, id: comment.id), method: :delete, data: { confirm: "Are you sure?" } do