Skip to content

Commit

Permalink
[ci skip][WIP]Create comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yucao24hours committed Sep 2, 2016
1 parent ceae597 commit a7d83b3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
22 changes: 22 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class CommentsController < ApplicationController
def index
@comments = Comment.all
end

def create
post = Post.find(params[:post_id])
@comment = post.comments.build(comment_params)

if @comment.save
redirect_to post, notice: 'Comment has been successfully created.'
else
redirect_to post, alert: 'Hoge!'
end
end

private

def comment_params
params.require(:comment).permit(:body).merge(commented_by: current_user)
end
end
1 change: 1 addition & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def index
end

def show
@comment = @post.comments.new
@comments = @post.comments
end

Expand Down
4 changes: 4 additions & 0 deletions app/views/posts/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
.author__created-at.grey-text.text-darken-1
= @post.created_at

= form_for @comment, url: post_comments_path(@post) do |f|
= f.text_field :body
= f.submit

- @comments.each do |comment|
.comment
.author
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
root to: 'posts#index'
resources :users, only: %i( index show )
resources :posts do
resources :comments, only: %i( index )
resources :comments, only: %i( index new create )
end

get 'login', to: 'sessions#new'
Expand Down

0 comments on commit a7d83b3

Please sign in to comment.