How to generate dynamic error page in Rails

How to generate dynamic error page in rails?

Step: #1

Run command for generating errors controller & view:

$ rails generate controller errors not_found internal_server_error unacceptable

Step: #2

Edit controller like below code:

class ErrorsController < ApplicationController
  def not_found
      render(:status => 404)

  def unacceptable
      render(:status => 422)

  def internal_server_error
      render(:status => 500)

Step: #3

Configure routes.rb

match "/404", to: "errors#not_found", via: :all
match "/500", to: "errors#internal_server_error", via: :all
match "/422", to: "errors#unacceptable", via: :all

Step: #4

Configure application.rb

config.exceptions_app = self.routes

Step: #5

Remove default template from public folder like 500.html, 404.html, 422.html

Step: #6

Restart your server then

Step: #7

Test localhost:3000/500, localhost:3000/404,  localhost:3000/422

Step: #8 For development

config.consider_all_requests_local = false


Happy coding!

Users Activity Tracking on Rails

How to enable users activity tracking using the public_activity gem?

On Rails, the public_activity provides easy way for tracking activity for ActiveRecord, Mongoid 3 and MongoMapper models. Simply we have to put records what has been changed or created and that will gives the ability to present those recorded activities to users. Following Github screenshot showing the expected output from the Rails users activity tracking-


Following steps we have to follow: (ActiveRecord only)

Step 1:

Add gem 'public_activity' on your Gemfile

Step 2:

Run bundle install

Step 3:

Run rails g public_activity:migration


Run rake db:migrate

Now let’s configure the Model

class Post < ActiveRecord::Base

include PublicActivity::Model




Custom activities

@post.create_activity key: 'post.commented_on', owner: current_user


View activities

#=> app/controller/notifications_controller.rb

def index

@activities = PublicActivity::Activity.all



That’s all for tracking all activities.

Happy coding 🙂