Error installing rmagick on Ubuntu

While running bundle install I am getting this error:

$ gem install rmagick Building native extensions. This could take a while...

ERROR: Error installing rmagick: ERROR: Failed to build gem native extension. Can't install RMagick 2.13.2. Can't find Magick-config

I have solve this problem by installing rmagick and libmagick++-dev package

Install imagemagick:

sudo apt-get install imagemagick

Install rmagick:

$ sudo apt-get install libmagick++-dev

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 :)

The program ‘bundle’ is currently not installed

This is a common problem when you first time install a new version of ruby. To fix this problem please follow below steps:

Step one:
Set up the PATH variable for the installed ruby executable
(Suppose the installed ruby is ruby-2.1.5):

Step two:


Step three:

gem install bundler

(if bundler is not installed)

Final Step:

rvm use ruby-2.1.5 --default

(–default is optional).

This will fix your problem.

Hope it helps :)

Remove model field name in rails validation

By default rails show field name before it’s default message.

For example default rails validation:

validates_xxx_of :the_column, :message =&gt; "Please add this!" will show message "The ColumnPlease add this!"

For project purposes we need some time to add custom error messages without a field name in rails validation.

We can solve this by adding custom method in model.

In rails 2: you can do on your model:

validate :test_validation


def test_validation

In rails 3 or greater:

validate :test_validation


def test_validation
errors.add(:base, 'Message')

vi – basic commands

vi is a screen-oriented text editor originally created for the Unix operating system.

Basic commands:

To Start vi:

* vi filename Create or Edit filename starting at line 1
* vi -r filename Recover filename that was being edited 

To Exit vi:

* :x<return> Quit vi, writing out modified file to file named in original invocation
* :wq<return> Quit vi, writing out modified file to file named in original invocation
* :q<return> Quit or exit vi
* :q!<return> Quit vi even though latest changes have not 

Moving the cursor:

* j or <return> [or down-arrow] Move cursor down one line
* k[or up-arrow] Move cursor up one line
* h or<backspace> [or left-arrow]

Move cursor left one line:

* l or<space>[or right-arrow] Move cursor right one line
* 0(zero) Move cursor to start of current line(the one with the arrow)
* $ Move cursor to end of the current line
* w Move cursor to beginning of next word Basic vi commands7. b Move cursor back to beginning of preceding word
* :0<return> or 1G Move cursor to first line in file
* :n<return>or nG Move cursor to line n in file
* :$<return>or G Move cursor to last line in file

Screen Manipulation:

* ^f Move forward one screen
* ^b Move backward one screen
* ^d Move down(forward) one half screen
* ^u Move up(back)one half sreen
* ^l Redraws the screen
* ^r Redraws the screen,removing the deleted lines 

Adding,Changing,Deleting the text:

* u Undo whatever you just did(a simple toggle)

Inserting or Adding text:

* i Insert text before cursor,until<esc>hit
* I Insert text at beginning of current line,until<esc>hit
* a Append text after cursor,until<esc>hit
* A Append text to end of current line,until<esc>hit
* o Open and put text in a new line below current line,until<esc>hit
* O Open and put text in a new line above current line,until<esc>hit

Basic vi commandsChanging the text:

* r Replace single character under cursor(no <esc>needed)
* R Replace characters,starting with current cursor position,until<esc>hit
* cw Change the current word with new text,starting with the character under cursor,until<esc>hit
* cNw Change N words beginning with the character under cursor,until<esc>hit
* c change(replace)the characters in the current line,until<esc>hit
* cc change(replace)the characters in the current line,stopping when <esc>hit
* Ncc or cNc change(replace)the next N lines,starting with the current line,stopping when <esc>hit

Deleting the text:

* x Delete single character under cursor
* Nx Delete N characters, starting with character under cursor
* dw Delete the single word beginning with character under cursor
* dNw Delete N words beginning with character under cursor
* D Delete the remainder of the line,starting with current cursor position
* dd Delete entire current line
* Ndd or dNd Delete N lines beginning with the current line

Basic vi commandsCutting and pasting the text:

* yy Copy (yank,cut) the current line into buffer
* Nyy or yNy copy(yank,cut) the next N lines,including the current line, into the buffer
* p put(paste) the lines in the buffer into the text after the current line

Searching the text:

* /string Search forward for occurence of string in text
* ?string Search backward for occurence of string in text
* n Move to next occurence of search string
* N Move to next occurence of search string in opposite direction

Determining line numbers:

* :.= Returns line number of current line at bottom of screen
* := Returns the total number of lines at bottom of the screen
* ^g Provides the current line number, along with the total number of lines,in the file at the bottom of the screen

Saving and reading files:

* :r filename<return> Read file named filename and insert after current line(the line with arrow) Basic vi commands2. :w<return> Write current contents to a file named in original vi call
* :w newfile<return> Write current contents to a new file named newfile
* :12,35w smallfile<return> Write the contents of the line numbered 12 through 35 to a new file named smallfile
* :w! prevfile<return> Write current contents over a pre-existing file named prevfile

Export XML file from rails console

One of my last project I need to export XML file for automate a data entry. I follow those simple steps to generate XML file from rails console.

Here is my script to generate XML file:

require 'builder'
@schools = School.where(state_code: 'NV').order('name DESC')
    file ="#{Rails.root}/public/data.xml", 'w')

    xml = file, :indent => 2)
    xml.instruct! :xml, :version=>'1.0'

    xml.tag! 'plist' , 'version' => '1.0' do
      xml.array do
        @schools.each do |s|
          xml.dict do
            xml.key 'id'
            xml.key 'name'
            xml.key 'zip'
            xml.string s.zipcode

Happy coding :)