Sha256: 85ad6ba862775bc7539e569cb61191712b2a4fe05f1625df7b6d99b7dd58d67e

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

require_dependency "form_creation/application_controller"

module FormCreation
  class PostsController < ApplicationController
    def index   
      @posts = Post.all   
    end   
     
    # GET method to get a Post by id   
    def show   
      @post = Post.find(params[:id])   
    end   
     
    # GET method for the new Post form   
    def new   
      @post = Post.new   
    end   
     
    # POST method for processing form data   
    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   
     
     # GET method for editing a Post based on id   
    def edit   
      @post = Post.find(params[:id])   
    end   
     
    # PUT method for updating in database a Post based on id   
    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   
     
    # DELETE method for deleting a Post from database based on id   
    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   
     
    # we used strong parameters for the validation of params   
    def post_params   
      params.require(:post).permit(:title, :description, :created_by)   
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
form_creation-0.0.3 app/controllers/form_creation/posts_controller.rb
form_creation-0.0.2 app/controllers/form_creation/posts_controller.rb