Last active
May 31, 2016 02:22
-
-
Save mfifth/194a9464839f56387bd1158d699af91e 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
| From: /home/ubuntu/workspace/app/views/topics/show.html.erb @ line 30 ActionView::CompiledTemplates#_app_views_topics_show_html_erb___4440374457857120152_70213913638600: | |
| 25: </ul> | |
| 26: | |
| 27: <br> | |
| 28: | |
| 29: <ul class="attributes"> | |
| => 30: <% binding.pry %> | |
| 31: <% if @topic.comments.any? %> | |
| 32: <%= @topic.comments.each do |comment| %> | |
| 33: <%= comment.author.email %> | |
| 34: <%= comment.text %> | |
| 35: <% end %> | |
| [1] pry(#<#<Class:0x007fb7e5ece678>>)> @topic | |
| => #<Topic:0x007fb7e64d01c0 | |
| id: 4, | |
| title: "Sonic has a new charbroiled burger", | |
| description: "Has anyone tried it yet? I hear it's supposed to taste really good.", | |
| forum_id: 1, | |
| created_at: Thu, 19 May 2016 00:46:33 UTC +00:00, | |
| updated_at: Thu, 19 May 2016 00:46:33 UTC +00:00, | |
| author_id: 1> | |
| [2] pry(#<#<Class:0x007fb7e5ece678>>)> @topic.comments.count | |
| (0.5ms) SELECT COUNT(*) FROM "comments" WHERE "comments"."topic_id" = ? [["topic_id", 4]] | |
| => 1 | |
| [3] pry(#<#<Class:0x007fb7e5ece678>>)> @topic.comments | |
| Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."topic_id" = ? [["topic_id", 4]] | |
| => [#<Comment:0x007fb7e5dfff30 | |
| id: 4, | |
| topic_id: 4, | |
| text: "Alright this is a comment created through the form.", | |
| created_at: Tue, 31 May 2016 00:11:04 UTC +00:00, | |
| updated_at: Tue, 31 May 2016 00:11:04 UTC +00:00, | |
| author_id: 2>, | |
| #<Comment:0x007fb7e6c720b8 id: nil, topic_id: 4, text: nil, created_at: nil, updated_at: nil, author_id: nil>] | |
| [4] pry(#<#<Class:0x007fb7e5ece678>>)> |
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 AddAuthorToComments < ActiveRecord::Migration | |
| def change | |
| add_reference :comments, :author, index: true | |
| add_foreign_key :comments, :users, column: :author_id | |
| 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
| class TopicsController < ApplicationController | |
| before_action :set_forum | |
| before_action :set_topic, only: [:update, :destroy, :edit, :show] | |
| before_action :verify_author, only: [:destroy, :update, :edit] | |
| before_action :authenticate_user!, only: [:create, :new] | |
| def index | |
| @topics = Topic.all | |
| end | |
| def new | |
| @topic = @forum.topics.build | |
| end | |
| def show | |
| @comment = @topic.comments.build | |
| end | |
| def edit | |
| end | |
| def create | |
| @topic = @forum.topics.build(topic_params) | |
| @topic.author = current_user | |
| if @topic.save | |
| flash[:notice] = "Topic has been successfully created." | |
| redirect_to [@forum, @topic] | |
| else | |
| flash.now[:alert] = "Topic has not been created." | |
| render 'edit' | |
| end | |
| end | |
| def destroy | |
| @topic.destroy | |
| flash[:notice] = "Topic has been deleted." | |
| redirect_to @forum | |
| end | |
| def update | |
| if @topic.update(topic_params) | |
| flash[:notice] = "Topic has been updated." | |
| redirect_to [@forum, @topic] | |
| else | |
| flash.now[:alert] = "Topic has not been updated." | |
| render 'edit' | |
| end | |
| end | |
| private | |
| def topic_params | |
| params.require(:topic).permit(:title, :description) | |
| end | |
| def set_forum | |
| @forum = Forum.find(params[:forum_id]) | |
| end | |
| def set_topic | |
| @topic = @forum.topics.find(params[:id]) | |
| end | |
| def verify_author | |
| unless current_user.email == @topic.author.email || current_user.admin? | |
| flash[:alert] = 'Only the author can edit or delete their own post.' | |
| redirect_to forums_path | |
| end | |
| 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
| <ul class="attributes"> | |
| <% if @topic.comments.any? %> | |
| <% @topic.comments.each do |comment| %> | |
| <li><h4><%= comment.author.email %> -</li></h4> | |
| <li><%= comment.text %></li> | |
| <% end %> | |
| <% else %> | |
| <p>There are no comments on this topic (yet!)</p> | |
| <% end %> | |
| </ul> | |
| <header>New Comment</header> | |
| <%= render 'comments/form' %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment