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



27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/form_creation/posts_controller.rb', line 27

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



56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/form_creation/posts_controller.rb', line 56

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



39
40
41
# File 'app/controllers/form_creation/posts_controller.rb', line 39

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

#indexObject



6
7
8
9
10
11
12
13
14
# File 'app/controllers/form_creation/posts_controller.rb', line 6

def index   
  @posts = Post.order('id ASC')
  @post = Post.new

  respond_to do |format|
    format.html
    format.xls { send_data @posts.to_csv(col_sep: "\t") }
  end   
end

#newObject

GET method for the new Post form



22
23
24
# File 'app/controllers/form_creation/posts_controller.rb', line 22

def new   
  @post = Post.new   
end

#post_paramsObject

we used strong parameters for the validation of params



68
69
70
# File 'app/controllers/form_creation/posts_controller.rb', line 68

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

#showObject

GET method to get a Post by id



17
18
19
# File 'app/controllers/form_creation/posts_controller.rb', line 17

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

#updateObject

PUT method for updating in database a Post based on id



44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/form_creation/posts_controller.rb', line 44

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