Class: CommentsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
lib/generators/make_commentable/templates/comments_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /comments POST /comments.json



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/generators/make_commentable/templates/comments_controller.rb', line 42

def create
  @source = find_commentable
  @comment = @source.comments.new(params[:comment])

  if params[:comment][:parent_id] != ''
    parent_comment = Comment.find(params[:comment][:parent_id])
    @comment.parent = parent_comment
  end

  @comment.user = current_user

  respond_to do |format|
    if @comment.save
      format.html { flash[:notice] = 'comment added'; redirect_to :back }
      format.js { render :layout => false }
    else
      format.html { flash[:error] = 'error happened'; render :action => 'new' }
      format.js { render :layout => false }
    end
  end
end

#destroyObject

DELETE /comments/1 DELETE /comments/1.json



82
83
84
85
86
87
88
89
90
# File 'lib/generators/make_commentable/templates/comments_controller.rb', line 82

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
end

#editObject

GET /comments/1/edit



36
37
38
# File 'lib/generators/make_commentable/templates/comments_controller.rb', line 36

def edit
  @comment = Comment.find(params[:id])
end

#indexObject

GET /comments GET /comments.json



4
5
6
7
8
9
10
11
# File 'lib/generators/make_commentable/templates/comments_controller.rb', line 4

def index
  @comments = Comment.all

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @comments }
  end
end

#newObject

GET /comments/new GET /comments/new.json



26
27
28
29
30
31
32
33
# File 'lib/generators/make_commentable/templates/comments_controller.rb', line 26

def new
  @comment = Comment.new

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @comment }
  end
end

#showObject

GET /comments/1 GET /comments/1.json



15
16
17
18
19
20
21
22
# File 'lib/generators/make_commentable/templates/comments_controller.rb', line 15

def show
  @comment = Comment.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @comment }
  end
end

#updateObject

PUT /comments/1 PUT /comments/1.json



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/generators/make_commentable/templates/comments_controller.rb', line 66

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
  end
end