0

Hi there I want to add a destroy action in post#show view I think I have creates the environment but when I place this code with a condition I have a message error.

<% 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 %>

I created a partial in post show#view which names _comments.html.erb

here it is

<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 %>

and it render like that <%= render 'comments' %>

and above the partial (in post show#view) I do an iteration like that

<ul class="list-unstyled">
    <% @post.comments.each do |comment| %>
   <li>
      <p><% comment.content %></p>
   <% end %>
   </li>
</ul>

But nothing appears when I create a new message, I don't userstand why.

I give your more code details

post.rb

has_many :comments, dependent: :destroy

comment.rb

belongs_to :user
belongs_to :post

The route is:

resources :posts do
  resources :categories
  resources :comments
end

Comments controller is

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

Thank you so much for your help.

4
  • 1
    <% if @user.comment == current_user %> How you are comparing this ? @user.comment returns the comment object and current_user is an user object. Commented Mar 29, 2016 at 12:32
  • 1
    <% link_to @post_comment_path(post_id: @post.id, id: comment.id), method: :delete, data: { confirm: "Are you sure?" } do %> should be <% link_to post_comment_path(post_id: @post.id, id: comment.id), method: :delete, data: { confirm: "Are you sure?" } do %> Commented Mar 29, 2016 at 12:34
  • I have to think @user.comment is the user who create the comment and is actually current_user as connected. Yes there is an error with that. Commented Mar 29, 2016 at 12:39
  • I tried what you did but there an error 'undefined local variable or method `comment' for #<#<Class:0x007fb154c64168>:0x007fb14d1e7b60>' Commented Mar 29, 2016 at 12:41

1 Answer 1

1

Your link_to erb code needs an equals sign for it to actually display

<%= link_to post_comment_path(post_id: @post.id, id: comment.id), method: :delete, data: { confirm: "Are you sure?" } do
Sign up to request clarification or add additional context in comments.

3 Comments

I have the error : undefined local variable or method comment'`
should probably be @comment.id
it works I put destroy method in the iteration <ul class="list-unstyled"> <% @post.comments.each do |comment| %> <li> <p><%= comment.content %></p> </li> <%= 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 %> </ul>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.