diff options
| author | mo k <mo@mokhan.ca> | 2012-06-07 22:23:58 -0600 |
|---|---|---|
| committer | mo k <mo@mokhan.ca> | 2012-06-07 22:23:58 -0600 |
| commit | dc05d508ccac71000f552435bab97bd8ba697bae (patch) | |
| tree | 456e81edc46c071713055f6f1603a6981f79b4f9 | |
| parent | c4aecda657b45842fac124e1f1f31a62bf754a5d (diff) | |
create comment controller and add form to creation#show page.
| -rw-r--r-- | app/controllers/comments_controller.rb | 18 | ||||
| -rw-r--r-- | app/views/creations/show.html.erb | 17 | ||||
| -rw-r--r-- | config/routes.rb | 1 | ||||
| -rw-r--r-- | spec/controllers/comments_controller_spec.rb | 18 |
4 files changed, 49 insertions, 5 deletions
diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 00000000..b1e2b7fe --- /dev/null +++ b/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 diff --git a/app/views/creations/show.html.erb b/app/views/creations/show.html.erb index 2769aace..3309d664 100644 --- a/app/views/creations/show.html.erb +++ b/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> diff --git a/config/routes.rb b/config/routes.rb index 6c58a612..20617bd6 100644 --- a/config/routes.rb +++ b/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' diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb new file mode 100644 index 00000000..7851d8c9 --- /dev/null +++ b/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 |
