Commit dc05d508

mo k <mo@mokhan.ca>
2012-06-08 04:23:58
create comment controller and add form to creation#show page.
1 parent c4aecda
Changed files (4)
app
controllers
views
creations
config
spec
app/controllers/comments_controller.rb
@@ -0,0 +1,18 @@
+class CommentsController < ApplicationController
+  before_filter :authenticate_user!
+
+  def new
+    @comment = Comment.new
+  end
+
+  def create
+    creation = Creation.find(params[:creation_id])
+    comment = Comment.build_from(creation, current_user, params[:comment][:body])
+    if comment.save
+      redirect_to creation_path(creation)
+    else
+      flash[:error] = "Ooops... we couldn't save your comment at this time."
+      render 'new'
+    end
+  end
+end
app/views/creations/show.html.erb
@@ -8,11 +8,11 @@
         <%= link_to 'Delete', creation_path(@creation),:confirm => "Are you sure", :method => :delete, :class => 'btn btn-danger' %> 
       <% end %>
       submitted by <%= link_to @creation.user.name, profile_path(@creation.user) %> 
-    </small> 
-  </h1>
-  <% @creation.categories.each do |category| %>
-    <span class="label"><a href="/categories/<%= category.slug %>"><%= category.name %></a></span>
-  <% end %> 
+  </small> 
+</h1>
+<% @creation.categories.each do |category| %>
+  <span class="label"><a href="/categories/<%= category.slug %>"><%= category.name %></a></span>
+<% end %> 
 </div>
 <div class="row">
   <div class="span12">
@@ -84,5 +84,12 @@
         </tr>
       <% end -%>
     </table>
+    <%= form_tag(creation_comments_path(@creation), :method => "post", :html => { :class => "form-horizontal"}) do |f| %>
+      <%= fields_for Comment.new do |f| %>
+        <label>Add a comment</label>
+        <%= f.text_area :body, :class => "span12" %>
+        <input type="submit" class="btn" value="Add Comment" />
+      <% end %>
+    <% end %>
   </div>
 </div>
config/routes.rb
@@ -14,6 +14,7 @@ Cake::Application.routes.draw do
   resources :creations do
     resources :photos, :only => [:create, :destroy]
     resources :favorites, :only => [:index, :create]
+    resources :comments, :only => [:index, :new, :create]
   end
   match 'creations/crop/:id' => 'creations#crop', :method => 'GET'
   match 'creations/crop_update/:id' => 'creations#crop_update', :as => 'creations_crop_update', :method => 'POST'
spec/controllers/comments_controller_spec.rb
@@ -0,0 +1,18 @@
+require_relative '../../app/controllers/comments_controller'
+require 'fakes'
+require 'fakes-rspec'
+
+describe CommentsController do
+  let(:sut) { CommentsController.new }
+  describe 'when commenting on a creation' do
+    let(:comment) { fake }
+
+    it "should save the new comment" do
+      comment.should have_receieved(:save)
+    end
+    before(:each) do
+      Comment.stub(:build_from).and_return(comment)
+      sut.create
+    end
+  end
+end