I want my visitors to be able to edit or delete their comment up too 5-10 min after they created it.
How should I authenticate this with a session or cookie?
My comment controller:
class CommentsController < ApplicationController
# GET /comments
# GET /comments.xml
# GET /comments/new
# GET /comments/new.xml
def new
@comment = Comment.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @comment }
end
end
# GET /comments/1/edit
def edit
@comment = Comment.find(params[:id])
end
# POST /comments
# POST /comments.xml
def create
@blog = Blog.find(params[:blog_id])
params[:comment][:ip] = request.remote_ip
@comment = @blog.comments.create!(params[:comment])
redirect_to @blog
end
# PUT /comments/1
# PUT /comments/1.xml
def update
@comment = Comment.find(params[:id])
respond_to do |format|
if @comment.update_attributes(params[:comment])
format.html { redirect_to(admin_comments_path, :notice => 'Comment was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /comments/1
# DELETE /comments/1.xml
def destroy
@comment = Comment.find(params[:id])
@comment.destroy
respond_to do |format|
format.html { redirect_to(admin_comments_url, :notice => 'Indlæg slettet') }
format.xml { head :ok }
end
end
end
store the saved comment’s id in the session and then at the time of delete or update, check the session for the comment’s id and compare the current-time with the comment’s created_at… this can go in a filter method.
Also, you can move the code of finding the comment with id in a filter and can follow DRY.
Here it goes: