68151

Allowing users to delete their own comments in rails 3

Question:

For each post, there are postcomments.

Here's what the model for the Postcomment looks like:

class Postcomment < ActiveRecord::Base attr_accessible :comment_content belongs_to :user belongs_to :post end

and the Post model

class Post < ActiveRecord::Base attr_accessible :content, :image, :comment_content belongs_to :user has_many :postcomments, dependent: :destroy end

I would like to allow users to delete their own postcomments. This is what I already have in the view

_postcomment.html.erb

<% if post.postcomments.exists? %> <% post.postcomments.each do |postcomment| %> <%= link_to postcomment.user.name, postcomment.user %> <span class="content2"><%= postcomment.postcomment_content %></span> <% end %> <% end %>

How should this code below be changed to allow users to delete comments?

<% if current_user?(postcomment.user) %> <%= link_to "delete", postcomment.content, method: :delete, confirm: "You sure?", title: postcomment.content %> <% end %>

current_user methods in Sessions helper

def current_user=(user) @current_user = user end def current_user @current_user ||= User.find_by_remember_token(cookies[:remember_token]) end def current_user?(user) user == current_user end

Here's the Postcomments table

create_table "postcomments", :force => true do |t| t.text "content" t.integer "user_id" t.integer "post_id" t.timestamp "created_at", :null => false t.timestamp "updated_at", :null => false t.text "comment_content" end

Answer1:

Perhaps...

if current_user == postcomment.user

If current_user returns a user instance anyways...

Answer2:

It might be better if you just compared the user id's. This is also easier to debug.

def current_user?(user) user.id == current_user.id end

Answer3:

Maybe something like:

<%= link_to("delete", postcomment.content, :method => :delete, :confirm => "You sure?", :title => postcomment.content) if postcomment.user == current_user %>

Recommend

  • Error: Sorry, upload functionality is currently not supported in iMacros for Chrome
  • grabbing the index value from the pointer when clicking on the image in MATLAB
  • Does MFC CList support the copy assignment?
  • How to instantiate tiles based on a 2D Array into a Platform Game using Unity?
  • animating a custom CALayer property
  • SQL - Find duplicate values and remove in a field
  • Printing in Silverlight 4 without Print Dialog - Out of Browser and elevated trust
  • Word Base/Stem Dictionary
  • Pythonic Way to have multiple Or's when conditioning in a dataframe
  • Template is missing error even if I have done a redirect_to in the controller
  • ruby on rails TypeError in Users#show - Cannot visit Like
  • Embedded or referenced relations
  • Rails has_many :through association: save instance into join table
  • std::string::substr throws std::out_of_range but the arguments are in limit
  • Give wx.StaticBitmap a transparent background? wxpython
  • Git subtree post hook
  • C++ how to get substring after get position of its index
  • how to add semantic ui in a rails app?
  • How to create a data template dependent on an XML Attribute?
  • How do I recognize a line break with a switch case that evaluates a char in Java?
  • Jquery Knockout: ko.computed() vs classic function?
  • Subclassing QGraphicsItem prevents me from being able to use itemAt() on a QGraphicsScene/View
  • accepts_nested_attributes_for practical form use for in Rails 3
  • Loading .coffee files via a view in Rails
  • How to create a file in java without a extension
  • Adding a button at the bottom of a table view
  • Getting last autonumber in access
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • MySQL WHERE-condition in procedure ignored
  • Cassandra Data Model
  • Delete MySQLi record without showing the id in the URL
  • align graphs with different xlab
  • Getting 'uninitialized constant' error when using delegate in belongs_to in model
  • GridView Sorting works once only
  • How to get icons for entities from eclipse?
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • JaxB to read class hierarchy
  • How to get NHibernate ISession to cache entity not retrieved by primary key