Class: FormCreation::PostsController

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

Instance Method Summary collapse

Instance Method Details

#createObject

POST method for processing form data



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

def create   
  @post = Post.new(post_params)
  if @post.save 
    flash[:notice] = 'Post added!'   
    redirect_to posts_path   
  else
    flash[:error] = 'Failed to edit Post!'   
    render :new
  end   
end

#destroyObject

DELETE method for deleting a Post from database based on id



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

def destroy   
  @post = Post.find(params[:id])   
  if @post.delete   
    flash[:notice] = 'Post deleted!'   
    redirect_to posts_path   
  else   
    flash[:error] = 'Failed to delete this Post!'   
    render :destroy   
  end   
end

#editObject

GET method for editing a Post based on id



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

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

#indexObject



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

def index   
  @posts = Post.order('id DESC')   
end

#newObject

GET method for the new Post form



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

def new   
  @post = Post.new   
end

#post_paramsObject

we used strong parameters for the validation of params



61
62
63
# File 'app/controllers/form_creation/posts_controller.rb', line 61

def post_params   
  params.permit(:title, :description, :created_by)   
end

#showObject

GET method to get a Post by id



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

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

#updateObject

PUT method for updating in database a Post based on id



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

def update   
  @post = Post.find(params[:id])   
  if @post.update_attributes(post_params)   
    flash[:notice] = 'Post updated!'   
    redirect_to posts_path   
  else   
    flash[:error] = 'Failed to edit Post!'   
    render :edit   
  end   
end