Sha256: 36ec98e5694ffd3c57c00f71ab5efa04ed33363b7e1a83292950bacfd1bd7be7

Contents?: true

Size: 1.7 KB

Versions: 10

Compression:

Stored size: 1.7 KB

Contents

require_dependency "form_creation/application_controller"

module FormCreation
  class PostsController < ApplicationController
    def index   
      @posts = Post.order('id DESC')   
    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.permit(:title, :description, :created_by)   
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
form_creation-0.1.5 app/controllers/form_creation/posts_controller.rb
form_creation-0.1.4 app/controllers/form_creation/posts_controller.rb
form_creation-0.1.3 app/controllers/form_creation/posts_controller.rb
form_creation-0.1.2 app/controllers/form_creation/posts_controller.rb
form_creation-0.1.1 app/controllers/form_creation/posts_controller.rb
form_creation-0.1.0 app/controllers/form_creation/posts_controller.rb
form_creation-0.0.9 app/controllers/form_creation/posts_controller.rb
form_creation-0.0.8 app/controllers/form_creation/posts_controller.rb
form_creation-0.0.7 app/controllers/form_creation/posts_controller.rb
form_creation-0.0.6 app/controllers/form_creation/posts_controller.rb