Permalink
Browse files

added comment functionality photos

  • Loading branch information...
1 parent 67acfcb commit f9acabe7eb671e3d72fc97f45960ed28cc7a713a @bcharna bcharna committed Apr 17, 2012
View
5 app/controllers/articles_controller.rb
@@ -6,7 +6,12 @@ def index
def show
+
@article = Article.find(params[:id])
+
+ @comments = @article.comments
+ @commentable = @article
+
end
View
104 app/controllers/comments_controller.rb
@@ -1,83 +1,65 @@
class CommentsController < ApplicationController
- # GET /comments
- # GET /comments.json
+
def index
- @comments = Comment.all
-
- respond_to do |format|
- format.html # index.html.erb
- format.json { render json: @comments }
- end
+ @commentable = find_commentable
+ @comments = @commentable.comments
end
-
- # GET /comments/1
- # GET /comments/1.json
+
def show
@comment = Comment.find(params[:id])
-
- respond_to do |format|
- format.html # show.html.erb
- format.json { render json: @comment }
- end
end
-
- # GET /comments/new
- # GET /comments/new.json
+
def new
@comment = Comment.new
-
- respond_to do |format|
- format.html # new.html.erb
- format.json { render json: @comment }
+ end
+
+ def create
+ @commentable = find_commentable
+ @comment = @commentable.comments.build(params[:comment])
+ if @comment.save
+ flash[:notice] = "Successfully created comment."
+ # We'd like to go back to the page where the comment was made. To
+ # do this we need to know what type of item we were commenting on
+ # and what particular item was being commented.
+ commenting_on = @comment.commentable_type.pluralize
+ id = @comment.commentable_id
+ # until the asset pipeline came along, the following worked well to get there:
+ # redirect_to :controller => commenting_on, :action => 'show', :id => id
+ # instead, we will simply use the following eval hack to go back to the page:
+ eval "redirect_to #{@comment.commentable_type.downcase}_path(:id=>#{id})"
+ else
+ render :action => 'new'
end
end
-
- # GET /comments/1/edit
+
def edit
@comment = Comment.find(params[:id])
end
-
- # POST /comments
- # POST /comments.json
- def create
- @comment = Comment.new(params[:comment])
-
- respond_to do |format|
- if @comment.save
- format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
- format.json { render json: @comment, status: :created, location: @comment }
- else
- format.html { render action: "new" }
- format.json { render json: @comment.errors, status: :unprocessable_entity }
- end
- end
- end
-
- # PUT /comments/1
- # PUT /comments/1.json
+
def update
@comment = Comment.find(params[:id])
-
- respond_to do |format|
- if @comment.update_attributes(params[:comment])
- format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
- format.json { head :no_content }
- else
- format.html { render action: "edit" }
- format.json { render json: @comment.errors, status: :unprocessable_entity }
- end
+ if @comment.update_attributes(params[:comment])
+ flash[:notice] = "Successfully updated comment."
+ redirect_to @comment
+ else
+ render :action => 'edit'
end
end
-
- # DELETE /comments/1
- # DELETE /comments/1.json
+
def destroy
@comment = Comment.find(params[:id])
@comment.destroy
-
- respond_to do |format|
- format.html { redirect_to comments_url }
- format.json { head :no_content }
- end
+ flash[:notice] = "Successfully destroyed comment."
+ redirect_to comments_url
end
+
+ private
+ def find_commentable
+ params.each do |name, value|
+ if name =~ /(.+)_id$/
+ return $1.classify.constantize.find(value)
+ end
+ end
+ nil
+ end
end
View
3 app/controllers/photos_controller.rb
@@ -7,6 +7,9 @@ def index
def show
@photo = Photo.find(params[:id])
+ @comments = @photo.comments
+ @commentable = @photo
+
end
View
2 app/models/article.rb
@@ -1,6 +1,8 @@
class Article < ActiveRecord::Base
# Relationship
belongs_to :category
+ has_many :comments, :as => :commentable, :dependent => :destroy
+
# Validations
validates_presence_of :title, :content
View
2 app/views/articles/show.html.erb
@@ -21,3 +21,5 @@
<%= link_to 'Edit', edit_article_path(@article), :class => 'btn' %>
<%= link_to 'Delete', article_path(@article), :method => 'delete', :confirm => 'Are you sure?', :class => 'btn btn-danger' %>
</div>
+
+<%= render :partial => "partials/comments_form", :locals => { obj: @photo }%>
View
3 app/views/photos/show.html.erb
@@ -22,3 +22,6 @@
<%= link_to 'Edit', edit_photo_path(@photo), :class => 'btn' %>
<%= link_to 'Delete', photo_path(@photo), :method => 'delete', :confirm => 'Are you sure?', :class => 'btn btn-danger' %>
</div>
+
+<%= render :partial => "partials/comments_list", :locals => { obj: @photo }%>
+<%= render :partial => "partials/comments_form", :locals => { obj: @photo }%>
View
9 config/routes.rb
@@ -9,5 +9,14 @@
match 'home' => 'home#index', :as => :home
root :to => 'home#index'
+
+ resources :articles do
+ resources :comments
+ end
+
+ resources :photos do
+ resources :comments
+ end
+
end
View
BIN db/development.sqlite3
Binary file not shown.
View
4 db/migrate/20120417003320_add_commentable.rb
@@ -1,6 +1,8 @@
class AddCommentable < ActiveRecord::Migration
def up
- t.references :commentable, :polymorhphic => true
+ alter_table :comments do |t|
+ t.references :commentable, :polymorhphic => true
+ end
end
def down
View
8 db/migrate/20120417005842_type.rb
@@ -0,0 +1,8 @@
+class Type < ActiveRecord::Migration
+ def up
+ add_column :comments, :commentable_type, :string
+ end
+
+ def down
+ end
+end
View
8 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120417000520) do
+ActiveRecord::Schema.define(:version => 20120417005842) do
create_table "articles", :force => true do |t|
t.string "title"
@@ -34,8 +34,10 @@
t.string "name"
t.string "email"
t.boolean "active"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.integer "commentable_id"
+ t.string "commentable_type"
end
create_table "photos", :force => true do |t|

0 comments on commit f9acabe

Please sign in to comment.