diff --git a/app/assets/javascripts/comments.js.coffee b/app/assets/javascripts/comments.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/comments.js.coffee @@ -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/ diff --git a/app/assets/stylesheets/comments.css.scss b/app/assets/stylesheets/comments.css.scss new file mode 100644 index 0000000..e730912 --- /dev/null +++ b/app/assets/stylesheets/comments.css.scss @@ -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/ diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..c15ee74 --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -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 diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb new file mode 100644 index 0000000..0ec9ca5 --- /dev/null +++ b/app/helpers/comments_helper.rb @@ -0,0 +1,2 @@ +module CommentsHelper +end diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb index c0358fc..4b98457 100644 --- a/app/views/articles/show.html.erb +++ b/app/views/articles/show.html.erb @@ -10,5 +10,34 @@ <%= @article.text %>

+

Comments

+<% @article.comments.each do |comment| %> +

+ Commenter: + <%= comment.commenter %> +

+ +

+ Comment: + <%= comment.body %> +

+<% end %> + +

Add a comment:

+ +<%= form_for([@article, @article.comments.build]) do |f| %> +

+ <%= f.label :commenter %>
+ <%= f.text_field :commenter %> +

+

+ <%= f.label :body %>
+ <%= f.text_area :body %> +

+

+ <%= f.submit %> +

+<% end %> + <%= link_to 'Back', articles_path %> | <%= link_to 'Edit', edit_article_path(@article) %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 2cc5a69..b15383d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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". diff --git a/test/controllers/comments_controller_test.rb b/test/controllers/comments_controller_test.rb new file mode 100644 index 0000000..2ec71b4 --- /dev/null +++ b/test/controllers/comments_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class CommentsControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/helpers/comments_helper_test.rb b/test/helpers/comments_helper_test.rb new file mode 100644 index 0000000..2518c16 --- /dev/null +++ b/test/helpers/comments_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class CommentsHelperTest < ActionView::TestCase +end