Monthly Archives: April 2015

Create custom error pages dynamically in ruby on rails 4

The default status code templates are served by a Rack exception application. You can override this to be any Rack compatible app. To override this you need to add following line in your application.rb file.

# config/application.rb
config.exceptions_app = self.routes

Now we need to create ErrorsController:

class ErrorsController < ApplicationController
  def show
    @exception = env['action_dispatch.exception']
    render action: request.path[1..-1]

  def write_to_log_file
    if @exception.present?
      logger.error("Error occured: #{@exception.message}, Status: #{status_code}\n Time: #{}")


  def status_code
    params[:code] || 500

In show method we have added a function to write the exception to log file.

Now create templates for each status code in errors directory. I have created three template for 404.html.erb, 422.html.erb and 500.html.erb

# app/views/errors/404.html.haml
%h1 404 - Not Found

Now create route for this errors:

# config/routes.rb
 %w( 404 422 500 ).each do |code|
    get code, :to => 'errors#show', :code => code

And that’s it for dynamic error page.

Happy coding 🙂

Create RSS from your ruby on rails application

For one my recent project need RSS feed. So I have created myself.

This will show you how to create a RSS feed that the Feed Validator considers valid.

First create a FeedsController to host the RSS feed.

class FeedsController < ApplicationController

  layout false

  def rss
    @posts = Post.published_posts.limit(50)


Then create a view in views/feeds/rss.rss.builder for RSS.

xml.rss :version => '2.0', 'xmlns:atom' => '' do do
    xml.title 'Feed title'
    xml.description 'Feed description' root_url
    xml.language 'en'
    xml.tag! 'atom:link', :rel => 'self', :type => 'application/rss+xml', :href => posts_url

    for post in @posts
      xml.item do
        xml.title post.title
        xml.pubDate(post.created_at.rfc2822) post_url(post)
        xml.guid post_url(post)
        xml.image_url post.image_url



Create a route to the feed in config/routes.rb:

get 'feed.rss', to: 'feeds#rss', :format => 'rss'

You can get browsers to auto-detect your Rails blog rss feed with a single line of Ruby on Rails code:

<%= auto_discovery_link_tag(:rss, "") %>

That’s it.

Happy Coding 🙂

Mysql ‘Got error -1 from storage engine’ error

Today I got an error while updating my mysql record . I got “Mysql ‘Got error -1 from storage engine’ error” every time I try to update.

Below are the logs I found from my mysql error logs:

MySQL Error log path: /var/log/mysql/error.log

InnoDB: A new raw disk partition was initialized or
InnoDB: innodb_force_recovery is on: we do not allow
InnoDB: database modifications by the user. Shut down
InnoDB: mysqld and edit my.cnf so that newraw is replaced
InnoDB: with raw, and innodb_force_... is removed.

Finally I got solution for that. To solve this issue follow below steps.

1. Stop your mysql server

 sudo service mysql stop 

2. Set innodb_force_recovery to 0 or remove this line from /etc/mysql/my.cnf

 innodb_force_recovery = 0 

3. Start your mysql server

 sudo service mysql start 

This will solve your problem.

Happy Coding 🙂