GVS
GVS

Reputation: 229

How is this 'if statement' incorrect?

I am trying to write an if statement in a dropdown menu that when clicked, will display the options "delete" and "edit" for the user who owns the post. While it will show the options "report post" and "block user" for all other users.

I added this to my posts controller and it works correctly.

  def destroy
    @post = Post.find(params[:id])
    if current_user == @post.user
        @post.destroy
    end
    redirect_to root_path
  end

I created this if statement in my dropdown and it does not work correctly. I tried to delete a post I own and seen "Report post", I then tried to delete another user's post and still seen "Report post". It's suppose to show me "Delete" when I try to delete my own posts.

<ul>
  <% if current_user == @post.user %>
    <li><%= link_to 'Delete post', post, :method => :delete %></li>
  <% else %>
    <li><a href"#">Report post</a></li>
  <% end %>
</ul>

Upvotes: 0

Views: 44

Answers (1)

Bartłomiej Gładys
Bartłomiej Gładys

Reputation: 4615

you have error here:

<li><%= link_to 'Delete post', post, :method => :delete %></li>

and you need that:

<li><%= link_to 'Delete post', @post, :method => :delete %></li>

or here:

<% if current_user == @post.user %>

and you need that:

<% if current_user == post.user %>

Upvotes: 1

Related Questions