From 79102e808f497ea3e0805daa6f18b86a5540309e Mon Sep 17 00:00:00 2001
From: Shay Arnett
Date: Sat, 22 Mar 2008 17:42:52 -0400
Subject: [PATCH] added can_edit? helper
---
app/controllers/posts.rb | 2 +-
app/helpers/global_helpers.rb | 3 +++
app/views/posts/edit.html.erb | 9 ++++++---
app/views/topics/show.html.erb | 2 +-
4 files changed, 11 insertions(+), 5 deletions(-)
diff --git a/app/controllers/posts.rb b/app/controllers/posts.rb
index 1d72e62..1218ba7 100644
--- a/app/controllers/posts.rb
+++ b/app/controllers/posts.rb
@@ -45,7 +45,7 @@ def update
@post = Post.find_by_id(params[:id])
raise NotFound unless @post
if @post.update_attributes(params[:post])
- redirect url(:post, @post)
+ redirect url(:forum_topic, @topic)
else
raise BadRequest
end
diff --git a/app/helpers/global_helpers.rb b/app/helpers/global_helpers.rb
index d4d691c..dc36ba0 100644
--- a/app/helpers/global_helpers.rb
+++ b/app/helpers/global_helpers.rb
@@ -1,5 +1,8 @@
module Merb
module GlobalHelpers
# helpers defined here available to all views.
+ def can_edit?
+ @post.user_id == current_user.id || is_admin?
+ end
end
end
diff --git a/app/views/posts/edit.html.erb b/app/views/posts/edit.html.erb
index 535101c..04e9365 100644
--- a/app/views/posts/edit.html.erb
+++ b/app/views/posts/edit.html.erb
@@ -1,3 +1,6 @@
-Posts controller, edit action
-
-Edit this file in app/views/posts/edit.html.erb
\ No newline at end of file
+<% form_for @post, :action => url(:forum_topic_post,@post) do %>
+ <% fieldset :legend => "Edit Post" do %>
+ <%= text_area_control :body, :label => "Body" %>
+ <%= submit_button "Submit" %>
+ <% end %>
+<% end %>
\ No newline at end of file
diff --git a/app/views/topics/show.html.erb b/app/views/topics/show.html.erb
index fce3214..4787f83 100644
--- a/app/views/topics/show.html.erb
+++ b/app/views/topics/show.html.erb
@@ -15,7 +15,7 @@
<%= post.user.login %>
<%= post.user.posts.size %> Posts
- <%= post.body_html %> |
+ <%= link_to "Edit", url(:edit_forum_topic_post,post) if can_edit? %> <%= post.body_html %> |
<% end %>