Permalink
Browse files

Edit/update a post, refactors comments form

  • Loading branch information...
Javier Fonseca V
Javier Fonseca V committed Nov 21, 2012
1 parent ce82632 commit a950a273279d3bf972b840dc813bfbe79a7d77db
@@ -1,5 +1,5 @@
class PostsController < ApplicationController
- before_filter :authenticate_user!, :only => [:new, :create]
+ before_filter :authenticate_user!, :except => [:index, :show]
def index
@posts = Post.order("created_at DESC")
@@ -8,6 +8,23 @@ def index
def show
@post = Post.find_by_id(params[:id])
@comments = @post.comments.order("created_at DESC")
+
+ @comment = @post.comments.build
+ end
+
+ def edit
+ @post = Post.find(params[:id])
+ end
+
+ def update
+ @post = Post.find(params[:id])
+
+ if @post.update_attributes(params[:post])
+ redirect_to posts_path, :notice => "Your post was edited succesfully"
+ else
+ flash[:error] = "There were some errors editing your post"
+ render action: "edit"
+ end
end
def new
@@ -0,0 +1,6 @@
+<%= simple_form_for [@post, @comment] do |f| %>
+ <h3>Insert a comment here:</h3>
+ <%= f.input :nickname %>
+ <%= f.input :message, :input_html => {:rows => 5} %>
+ <%= f.button :submit %>
+<% end %>
@@ -0,0 +1,7 @@
+<h1>Edit a Post</h1>
+
+<% if @post.errors.any? %>
+ <%= flash[:error] %>
+<% end %>
+
+<%= render 'form' %>
@@ -10,8 +10,5 @@
<% end %>
</ul>
-<%= simple_form_for [@post, @post.comments.build] do |f| %>
- <%= f.input :nickname %>
- <%= f.input :message, :input_html => {:rows => 5} %>
- <%= f.button :submit %>
-<% end %>
+
+<%= render "comments/form" %>

0 comments on commit a950a27

Please sign in to comment.