Class: OCms::PostsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/o_cms/posts_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/o_cms/posts_controller.rb', line 19

def create
  @post = Post.new(post_params)

  if @post.save
    flash[:notice] = "Post was saved successfully."
    redirect_to edit_post_path(@post)
  else
    flash.now[:alert] = "Error creating post. Please try again."
    render :new
  end
end

#destroyObject



49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/o_cms/posts_controller.rb', line 49

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

  if @post.destroy
    flash[:notice] = "\"#{@post.title}\" was deleted successfully."
    redirect_to action: :index
  else
    flash.now[:alert] = "There was an error deleting the post."
    render :show
  end
end

#editObject



31
32
33
34
# File 'app/controllers/o_cms/posts_controller.rb', line 31

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

#indexObject



6
7
8
# File 'app/controllers/o_cms/posts_controller.rb', line 6

def index
  @posts = Post.all
end

#newObject



14
15
16
17
# File 'app/controllers/o_cms/posts_controller.rb', line 14

def new
  @post = Post.new
  @categories = Category.all
end

#showObject



10
11
12
# File 'app/controllers/o_cms/posts_controller.rb', line 10

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

#updateObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/o_cms/posts_controller.rb', line 36

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

  if @post.save
    flash[:notice] = "Post was updated successfully."
    redirect_to edit_post_path(@post)
  else
    flash.now[:alert] = "Error saving post. Please try again."
    render :edit
  end
end