Class: PostsController

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

Instance Attribute Summary

Attributes inherited from ApplicationController

#req, #res

Instance Method Summary collapse

Methods inherited from ApplicationController

#boolean_param, #csrf_token, #delete_all_images_from_content, #delete_from_storage, #initialize, #params, #redirect_to, #render, #session, #validate!

Constructor Details

This class inherits a constructor from ApplicationController

Instance Method Details

#createObject



14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/posts_controller.rb', line 14

def create
  data = params['post']
  data['is_active'] = boolean_param(data['is_active'])
  @post = Post.new(data)
  if @post.save
    redirect_to '/dashboard/posts'
  else
    render 'cms/posts_form'
  end
end

#destroyObject



41
42
43
44
45
46
47
# File 'app/controllers/posts_controller.rb', line 41

def destroy
  @post = Post[params['id']]
  delete_from_storage(@post.image_url) if @post.respond_to?(:image_url)
  delete_all_images_from_content(@post.content) if @post.respond_to?(:content)
  @post.destroy
  redirect_to '/dashboard/posts'
end

#editObject



25
26
27
28
# File 'app/controllers/posts_controller.rb', line 25

def edit
  @post = Post[params['id']]
  render 'cms/posts_form'
end

#indexObject



4
5
6
7
# File 'app/controllers/posts_controller.rb', line 4

def index
  @posts = Post.order(Sequel.desc(:created_at)).all
  render 'cms/posts_index'
end

#newObject



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

def new
  @post = Post.new
  render 'cms/posts_form'
end

#updateObject



30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/posts_controller.rb', line 30

def update
  @post = Post[params['id']]
  data = params['post']
  data['is_active'] = boolean_param(data['is_active'])
  if @post.update(data)
    redirect_to '/dashboard/posts'
  else
    render 'cms/posts_form'
  end
end