Class: PostsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
lib/generators/forum_monster/templates/controllers/posts_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/generators/forum_monster/templates/controllers/posts_controller.rb', line 14

def create
  @topic = Topic.find(params[:topic_id])
  @post = @topic.posts.build(params[:post])
  @post.forum = @topic.forum
  @post.user = current_user
  
  if @post.save
    flash[:notice] = "Post was successfully created."
    redirect_to topic_path(@post.topic)
  else
    render :action => 'new'
  end
end

#destroyObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/generators/forum_monster/templates/controllers/posts_controller.rb', line 41

def destroy
  @post = Post.find(params[:id])
  
  if @post.topic.posts_count > 1
    if @post.destroy
      flash[:notice] = "Post was successfully destroyed."
      redirect_to topic_path(@post.topic)
    end
  else
    if @post.topic.destroy
      flash[:notice] = "Topic was successfully deleted."
      redirect_to forum_path(@post.forum)
    end
  end
end

#editObject



28
29
30
# File 'lib/generators/forum_monster/templates/controllers/posts_controller.rb', line 28

def edit
  @post = Post.find(params[:id])
end

#newObject



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/generators/forum_monster/templates/controllers/posts_controller.rb', line 2

def new
  @topic = Topic.find(params[:topic_id])
  @post = Post.new
  
  if params[:quote]
    quote_post = Post.find(params[:quote])
    if quote_post
      @post.body = quote_post.body
    end
  end
end

#updateObject



32
33
34
35
36
37
38
39
# File 'lib/generators/forum_monster/templates/controllers/posts_controller.rb', line 32

def update
  @post = Post.find(params[:id])

  if @post.update_attributes(params[:post])
    flash[:notice] = "Post was successfully updated."
    redirect_to topic_path(@post.topic)
  end
end