Skip to content

Commit

Permalink
Comment controller, list and create form were created
Browse files Browse the repository at this point in the history
  • Loading branch information
tayfunoziserikan committed Aug 18, 2014
1 parent ace2d12 commit 3f6b14a
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/assets/javascripts/comments.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/comments.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Comments controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
15 changes: 15 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CommentsController < ApplicationController

def create
@article = Article.find(params[:article_id])
@comment = @article.comments.create(comment_params)
redirect_to article_path(@article)
end

private

def comment_params
params.require(:comment).permit(:commenter, :body)
end

end
2 changes: 2 additions & 0 deletions app/helpers/comments_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CommentsHelper
end
29 changes: 29 additions & 0 deletions app/views/articles/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,34 @@
<%= @article.text %>
</p>

<h2>Comments</h2>
<% @article.comments.each do |comment| %>
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>

<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
<% end %>

<h2>Add a comment:</h2>

<%= form_for([@article, @article.comments.build]) do |f| %>
<p>
<%= f.label :commenter %><br>
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>

<%= link_to 'Back', articles_path %>
| <%= link_to 'Edit', edit_article_path(@article) %>
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
Rails.application.routes.draw do
get 'welcome/index'

resources :articles
resources :articles do
resources :comments
end

# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
Expand Down
7 changes: 7 additions & 0 deletions test/controllers/comments_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class CommentsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
4 changes: 4 additions & 0 deletions test/helpers/comments_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class CommentsHelperTest < ActionView::TestCase
end

0 comments on commit 3f6b14a

Please sign in to comment.