Merhaba, post # show görünümünde bir yok etme eylemi eklemek istiyorum Çevreyi oluşturduğumu düşünüyorum ama bu kodu bir koşulla yerleştirdiğimde bir mesajım hatası var. Sözdizimi Raylar beklenmedik ')'
<% 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 %>
Ben
<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 %>
burada isimleri _comments.html.erb
olduğu sonrası gösteri # görünümünde kısmi oluşturulan ve bu <%= render 'comments' %>
ve üzeri gibi işlemek kısmi (post show # görünümünde)
<ul class="list-unstyled">
<% @post.comments.each do |comment| %>
<li>
<p><% comment.content %></p>
<% end %>
</li>
</ul>
gibi bir yineleme yapıyorum
Ancak, yeni bir ileti oluşturduğumda hiçbir şey görünmüyor, neden kullanmıyorsunuz?
Ben
post.rb sizin daha kod ayrıntıları vermek
has_many :comments, dependent: :destroy
comment.rb
belongs_to :user
belongs_to :post
yoldur:
resources :posts do
resources :categories
resources :comments
end
Yorumlar kontrolör
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
Yardımlarınız için çok teşekkür ederim olduğunu.
'<% if @ user.comment == örnein%>' Bunu nasıl karşılaştırdığınız görüntülemek için bir Eşittir işareti ihtiyacı var? @ user.comment, comment nesnesini döndürür ve "current_user" bir kullanıcı nesnesidir. – dp7
'<% link_to @post_comment_path (post_id: @ post.id, id: comment.id), yöntem:: sil, veri: {confirm:" Emin misiniz? " } do%> 'olmalıdır <% link_to post_comment_path (post_id: @ post.id, id: comment.id), yöntem:: sil, veri: {confirm:" Emin misiniz? " } do%> ' – dp7
@ user.commentin, yorumu oluşturan ve bağlı olarak current_user olduğunu düşünmek zorundayım. Evet, bununla ilgili bir hata var. –