Last active
May 26, 2016 23:10
-
-
Save mfifth/df8622b1e9b5803274c4cba1830e7147 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Comment < ActiveRecord::Base | |
| belongs_to :topic | |
| belongs_to :author, class_name: "User" | |
| validates :text, presence: true, length: { minimum: 15 } | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CommentsController < ApplicationController | |
| before_action :set_ticket | |
| before_action :set_comment, only: [:edit, :update] | |
| def edit | |
| end | |
| def update | |
| end | |
| def create | |
| @comment = @topic.comments.build(comment_params) | |
| @comment.author = current_user | |
| if @comment.save | |
| flash[:notice] = "Comment has been created." | |
| redirect_to [@topic.forum, @topic] | |
| else | |
| flash.now[:alert] = "Comment has not been created." | |
| render 'new' | |
| end | |
| end | |
| private | |
| def set_topic | |
| @topic = Topic.find(params[:topic_id]) | |
| end | |
| def set_comment | |
| @comment = Comment.find(params[:id]) | |
| end | |
| def comment_params | |
| params.require(:comment).permit(:text) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # The error message | |
| Showing /home/ubuntu/workspace/app/views/topics/show.html.erb where line #35 raised: | |
| undefined method `author' for nil:NilClass | |
| Extracted source (around line #12): | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| def comment_author_only(&block) | |
| block.call if current_user.email == @comment.author.email || | |
| current_user.try(:admin?) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div class='page-header'> | |
| <h1><%= @topic.title %></h1> | |
| </div> | |
| <table id='attributes'> | |
| <tr> | |
| <th>Author:</th> | |
| <td><%= @topic.author.email %></td> | |
| </tr> | |
| <tr> | |
| <th>Created: </th> | |
| <td> <%= time_ago_in_words(@topic.created_at) %> ago</td> | |
| </tr> | |
| </table> | |
| <h4><%= @topic.description %></h4> | |
| <ul class='actions'> | |
| <li><%= link_to "Back", forum_path(@forum), class: 'btn btn-primary' %></li> | |
| <li><%= link_to "Add Comment", new_topic_comment_path(@topic, @comment), class: "new btn-primary" %></li> | |
| <% topic_author_only do %> | |
| <li><%= link_to 'Edit Topic', edit_forum_topic_path, class: "edit" %></li> | |
| <li><%= link_to "Delete Topic", [@forum, @topic], method: :delete, | |
| data: { confirm: "Are you sure you want to delete this topic?" }, class: "delete btn-danger" %></li> | |
| <% end %> | |
| </ul> | |
| <br> | |
| <ul class="attributes"> | |
| <% if @topic.comments.any? %> | |
| <% @topic.comments.each do |comment| %> | |
| <li><h4><%= comment.author.email %> -</li></h4> | |
| <li><%= comment.text %></li> | |
| <% comment_author_only do %> | |
| <li><%= link_to "Edit Comment", edit_topic_comment_path(@topic, @comment), class: "edit" %></li> | |
| <% end %> | |
| <% end %> | |
| <% else %> | |
| <p>There are no comments on this topic (yet!)</p> | |
| <% end %> | |
| </ul> | |
| <header>New Comment</header> | |
| <%= render 'comments/form', ticket: @ticket, comment: @comment %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment