Created
September 2, 2012 16:38
-
-
Save telagraphic/3601283 to your computer and use it in GitHub Desktop.
Rails Comments
This file contains 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_filter :get_post | |
def get_post | |
@post = Post.find(params[:post_id]) # comment model has this table, so why are we calling it on post db? | |
end | |
def create | |
@comment = @post.comments.new(params[:comment]) | |
if @comment.save | |
flash[:notice] = "Comment added" | |
redirect_to @post | |
else | |
flash[:error] = "Screwed up!" | |
render "posts/show" | |
end | |
end | |
def destroy | |
@comment = @post.comments(params[:id]) | |
@comment.destroy | |
redirect_to @post | |
end | |
end |
This file contains 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 PostsController < ApplicationController | |
before_filter :get_post, only: [:show, :edit, :update, :destroy] | |
def get_post | |
@post = Post.find(params[:id]) | |
end | |
def index | |
@posts = Post.all | |
end | |
def show | |
@new_comment = @post.comments.build | |
@comments = @post.comments.all | |
# @comment = @post.comments.find(params[:id]) trying to access each individual comment on its parent post | |
end | |
def new | |
@post = Post.new | |
end | |
def create | |
@post = Post.new(params[:post]) | |
if @post.save | |
flash[:notice] = "Successfully created a new post" | |
redirect_to posts_path | |
else | |
flash[:error] = "Check yourself, you just wrecked yourself" | |
render "new" | |
end | |
end | |
def edit | |
end | |
def update | |
if @post.update_attributes(params[:post]) | |
flash[:notice] = "Successfully updated a new post" | |
redirect_to @post | |
else | |
flash[:error] = "Check yourself, cause you wrecked yourself" | |
render "edit" | |
end | |
end | |
def destroy | |
@post.destroy | |
flash[:notice] = "Post Deleted" | |
redirect_to posts_path | |
end | |
end |
This file contains 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
Blog::Application.routes.draw do | |
resources :posts do | |
resources :comments | |
end | |
root :to => "posts#index" | |
end |
This file contains 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
<%= link_to "Home", posts_path %> | |
<section class="posts"> | |
<article class="post"> | |
<h3><%= @post.title %></h3> | |
<h4><%= @post.created_at.strftime("%d %b. %Y")%></h4> | |
<iframe width="560" height="315" src="<%= @post.youtube %>" frameborder="0" allowfullscreen></iframe> | |
<p><%= @post.content %></p> | |
</article> | |
</section> | |
<%= link_to "Edit", edit_post_path(@post) %> | |
<%= link_to "Delete", @post, :confirm => "Double-Check?", :method => :delete %> | |
<hr> | |
<section class="new-comment"> | |
<%= render "comments/comment_form" %> | |
</section> | |
<section class="comments"> | |
<% @comments.each do |comment| %> | |
<header class="comment-header"><%= comment.name %></header> | |
<p><%= comment.comment %></p> | |
<%= link_to "Edit", edit_comment_path(comment) %> | |
<hr> | |
<% end %> | |
</section> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment