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
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 |
#destroy ⇒ Object
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 |
#edit ⇒ Object
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 |
#index ⇒ Object
5 6 7 |
# File 'app/controllers/form_creation/posts_controller.rb', line 5 def index @posts = Post.order('id DESC') end |
#new ⇒ Object
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_params ⇒ Object
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 |
#show ⇒ Object
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 |
#update ⇒ Object
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 |