Class: FormCreation::PostsController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- FormCreation::PostsController
- Defined in:
- app/controllers/form_creation/posts_controller.rb
Instance Method Summary collapse
-
#create ⇒ Object
POST method for processing form data.
-
#destroy ⇒ Object
DELETE method for deleting a Post from database based on id.
-
#edit ⇒ Object
GET method for editing a Post based on id.
- #index ⇒ Object
-
#new ⇒ Object
GET method for the new Post form.
-
#post_params ⇒ Object
we used strong parameters for the validation of params.
-
#show ⇒ Object
GET method to get a Post by id.
-
#update ⇒ Object
PUT method for updating in database a Post based on id.
Instance Method Details
#create ⇒ Object
POST method for processing form data
21 22 23 24 25 26 27 28 29 30 |
# File 'app/controllers/form_creation/posts_controller.rb', line 21 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 |
#destroy ⇒ Object
DELETE method for deleting a Post from database based on id
50 51 52 53 54 55 56 57 58 59 |
# File 'app/controllers/form_creation/posts_controller.rb', line 50 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 |
#edit ⇒ Object
GET method for editing a Post based on id
33 34 35 |
# File 'app/controllers/form_creation/posts_controller.rb', line 33 def edit @post = Post.find(params[:id]) end |
#index ⇒ Object
5 6 7 8 |
# File 'app/controllers/form_creation/posts_controller.rb', line 5 def index @posts = Post.order('id DESC') @post = Post.new end |
#new ⇒ Object
GET method for the new Post form
16 17 18 |
# File 'app/controllers/form_creation/posts_controller.rb', line 16 def new @post = Post.new end |
#post_params ⇒ Object
we used strong parameters for the validation of params
62 63 64 |
# File 'app/controllers/form_creation/posts_controller.rb', line 62 def post_params params.permit(:title, :description, :created_by) end |
#show ⇒ Object
GET method to get a Post by id
11 12 13 |
# File 'app/controllers/form_creation/posts_controller.rb', line 11 def show @post = Post.find(params[:id]) end |
#update ⇒ Object
PUT method for updating in database a Post based on id
38 39 40 41 42 43 44 45 46 47 |
# File 'app/controllers/form_creation/posts_controller.rb', line 38 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 |