Sunday, July 15, 2012

Generating Excel file with 'writeexcel' gem & 'axlsx' gem

Well, here I am describing a very short example of creating excel files in ruby. For this example, I have used the gem "writeexcel" writeexcel.

Here is a very small example

Well, Now a small example for generating excel file with axlsx gem.


Sunday, June 17, 2012

Could not find a JavaScript runtime (ExecJS::RuntimeUnavailable)

Hi,  

If you are using 'execjs' gem and if you get the below mentioned error, while starting the application server or executing rake tasks or even starting the console, then you might have to add the gem 'therubyracer'. 

Today, I was getting the following error in my system after installing 'execjs' gem:

/home/saurav/.rvm/gems/ruby-1.9.2-p318/gems/execjs-1.4.0/lib/execjs/runtimes.rb:51:in `autodetect': Could ot find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
from /home/saurav/.rvm/gems/ruby-1.9.2-p318/gems/execjs-1.4.0/lib/execjs.rb:5:in `<module:ExecJS>'
from /home/saurav/.rvm/gems/ruby-1.9.2-p318/gems/execjs-1.4.0/lib/execjs.rb:4:in `<top (required)>'
from /home/saurav/.rvm/gems/ruby-1.9.2-p318/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `require'
from /home/saurav/.rvm/gems/ruby-1.9.2-p318/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `<top (required)>'
from /home/saurav/.rvm/gems/ruby-1.9.2-p318/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `require'
from /home/saurav/.rvm/gems/ruby-1.9.2-p318/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `<top (required)>'
from /home/saurav/.rvm/gems/ruby-1.9.2-p318/gems/coffee-rails-3.2.2/lib/coffee-rails.rb:1:in `require'
from /home/saurav/.rvm/gems/ruby-1.9.2-p318/gems/coffee-rails-3.2.2/lib/coffee-rails.rb:1:in `<top (required)>'
.......
.......

The solution was to add the following line gem 'therubyracer' to the Gemfile and then execute 

bundle install

Voila !!....I don't see the error any more :) 




Tuesday, May 1, 2012

Referring to class from class method and instance method

Instead of directly writing ClassName for calling methods, this can be done in a different way by writing self.class

Below is a small example: 

Output will be:

Class Name is SomeClass

Class Name is SomeClass

Wednesday, April 25, 2012

Change Column or column type in Rails

Hi, 

If you have to change the column_type of an existing column in a table, you have to create a new migration and do the following changes:

saurav@saurav-Vostro-1540:~/workspace/project$./script/generate migration ChangeNotes

The above command will generate a new migration file and the migration should be like this after you have made the change:

class ChangeNotes < ActiveRecord::Migration
  def self.up
      change_table :notes  do |t|
        t.change :note, :text
       end
    end

    def self.down
      change_table :notes do |t|
        t.change :note, :string
      end
    end
end

where "note" is the column for which I have changed the data-type. Earlier, the data type of "note" column was set to "String" and now it has been set to "text"..

Thursday, February 16, 2012

RVM - No same user error fix

Hi All, 

Today, after installing RVM in my system i was getting a strange error "no same user error" whenever I was trying to install ruby with rvm. I was getting error of similar sort: 
"Error opening archive: Failed to open '--no-same-owner': No such file or directory"
It is actually a bug in rvm, see this commit: 
https://github.com/wayneeseguin/rvm/commit/52018750763d5321b7b993e201c8589b98e090f9 
Finally after some googling, I could solve the issue with the following command:
[administrator@bdsi989-linux administrator]$ rvm get head
Hope this helps some one :)

Thursday, December 22, 2011

Take Screenshots in Linux/Fedora

Many a times we need to take screenshot. Today, I have installed kdegraphics for having the screenshot feature in my Fedora v14 OS.
The installation procedure is pretty simple :)


You simply have to do sudo yum install kdegraphics and it will install the application for you along with the required dependencies. 

Now you can take screenshots. You can launch the application from Applications->Accessories->Take Screenshot

You will get 3 different options for taking the screenshot.

Wednesday, December 21, 2011

POSTGRESQL: Ident authentication error

Today,

I was trying to configure POSTGRESQL server in Fedora Linux OS system. On my surprise, when I typed:

# psql -d railsapp_development -U rails_db_user -W

It gave me the following error:

# psql: FATAL: Ident authentication failed for user "username" 

For, solving this issue, you will have to edit the following file:

vim /var/lib/pgsql/data/pg_hba.conf

This file has various functionalites apart from controlling how the user/clients are  authenticated. By default, POSTGRESQL, supporst IDENT based authentication. IDENT based authentication will not allow you to login with -U and -W option. 

The following changes has to be done inside pg_hba.conf file:

# Type    Database     User     CIDR-ADDRESS            METHOD
----------------------------------------------------------------------------------------------------
  local        all               all                                                   trust
  host         all               all          127.0.0.1/32                     trust 


Basically you will have to replace ident with trust. 

Now restart the server with the following command:

# /etc/init.d/postgresql restart

After this you will be able to login. 

Note, that I was facing the above mentioned error, when I was trying to invoke the command rake db:migrate in rails.


But after doing the above mentioned changes, migrations started working fine.