Browse files

Comments controller

  • Loading branch information...
1 parent 0579914 commit 24c3e3d92f6807bb74155667d8619c4d1727e358 @dce committed May 31, 2011
View
13 app/controllers/comments_controller.rb
@@ -0,0 +1,13 @@
+class CommentsController < ApplicationController
+ def create
+ @post = Post.find(params[:post_id])
+ @comment = Comment.new(params[:comment])
+ @comment.post = @post
+
+ if @comment.save
+ redirect_to @post
+ else
+ render :template => "posts/show"
+ end
+ end
+end
View
1 app/controllers/posts_controller.rb
@@ -5,5 +5,6 @@ def index
def show
@post = Post.published.find(params[:id])
+ @comment = Comment.new
end
end
View
7 app/views/posts/index.html.erb
@@ -4,6 +4,11 @@
<%= simple_format post.body %>
- <p><em><%= post.created_at.to_s(:long) %></em></p>
+ <p>
+ <em>
+ <%= post.created_at.to_s(:long) %> &middot;
+ <%= link_to pluralize(post.comments.count, "comment"), post_path(post, :anchor => "comments") %>
+ </em>
+ </p>
</div>
<% end %>
View
35 app/views/posts/show.html.erb
@@ -4,4 +4,39 @@
<%= simple_format @post.body %>
<p><em><%= @post.created_at.to_s(:long) %></em></p>
+
+ <h3 id="comments">Comments</h3>
+
+ <ul>
+ <% @post.comments.each do |comment| %>
+ <li>
+ <%= simple_format comment.comment %>
+
+ <p><em>&ndash; <%= comment.name %></em></p>
+ </li>
+ <% end %>
+ </ul>
+
+ <% form_for [@post, @comment] do |form| %>
+ <%= error_messages_for :comment %>
+
+ <p>
+ <%= form.label :name %><br />
+ <%= form.text_field :name %>
+ </p>
+
+ <p>
+ <%= form.label :email %><br />
+ <%= form.text_field :email %>
+ </p>
+
+ <p>
+ <%= form.label :comment %><br />
+ <%= form.text_area :comment %>
+ </p>
+
+ <p>
+ <%= form.submit %>
+ </p>
+ <% end %>
</div>
View
4 config/routes.rb
@@ -1,5 +1,7 @@
RailsAdminInterfaces::Application.routes.draw do
root :to => "posts#index"
- resources :posts, :only => [:index, :show]
+ resources :posts, :only => [:index, :show] do
+ resources :comments, :only => :create
+ end
end
View
8 test/functional/comments_controller_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class CommentsControllerTest < ActionController::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end

0 comments on commit 24c3e3d

Please sign in to comment.