Skip to content

Commit

Permalink
fix #384: allow commenting on a point (#386)
Browse files Browse the repository at this point in the history
* fix #384: allow commenting on a point

* clean up comments adding
  • Loading branch information
michielbdejong authored May 25, 2018
1 parent 0718638 commit e8b2232
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 0 deletions.
30 changes: 30 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class CommentsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
before_action :set_point, only: [:new, :create]
def new
@comment = Comment.new
end

def create
@comment = Comment.new(comment_params)
@comment.point = @point

if @comment.save
flash[:notice] = "Comment added!"
else
flash[:notice] = "Error adding comment!"
puts @comment.errors.full_messages
end
redirect_to point_path(@point)
end

private

def set_point
@point = Point.find(params[:point_id])
end

def comment_params
params.require(:comment).permit(:summary)
end
end
2 changes: 2 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class Comment < ApplicationRecord
belongs_to :point

validates :summary, presence: true
end
16 changes: 16 additions & 0 deletions app/views/comments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="form-login">
<div class="row row-form">
<%= simple_form_for([@point, @comment]) do |f| %>
<div class="form-inputs col-xs-12">
<%= f.input :summary %>
</div>
<div class="form-actions col-xs-4 col-sm-2 col-md-2">
<%= link_to "Back", :back, class: "btn btn-default" %>
</div>
<div class="form-actions col-xs-8 col-sm-4 col-sm-offset-6 col-md-3 col-md-offset-7">
<%= f.submit class: 'btn btn-primary' %>
</div>
<% end %>
</div>
</div>

2 changes: 2 additions & 0 deletions app/views/comments/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%= render 'form' %>

1 change: 1 addition & 0 deletions app/views/points/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<% end %>
<% end %>
</li>
<li><%= link_to 'Add Comment', new_point_comment_path(@point) %></li>
</ul>
</div>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
resources :points, only: :index, path: "points/(:scope)", scope: /[a-z\-_]*/, as: :points
resources :points, except: [:index] do
resources :reasons, only: [:new, :create]
resources :comments, only: [:new, :create]
end

resources :services, except: [:show]
Expand Down

0 comments on commit e8b2232

Please sign in to comment.