Saturday, December 31, 2011

HTTP basic authentication using email address

In my previous article, I have written about HTTP basic authentication in rails using a plain user access YML.

Last few days ago, I have modified the logic to log-in using email address and password. So that anyone don't need to remember another new password. Hope it might help you...

Written a news private method
def login_by_email(user_name, password)
    is_logged_in = false
    if (user_name.match(/.+\$\b/i))
      require 'net/imap'
      require 'openssl'
      client =, port, true, nil, false)
        client.login(user_name, password)
        is_logged_in = true
      rescue Exception => error
        logger.error "Unable to log-in :: #{error.message}"
    return is_logged_in
On line#3 I have checked the domain name to provide access for a specific domain. After that modified the authentication method following way:
def authenticate_for_staging_server
    authenticate_or_request_with_http_basic do |user_name, password|
      login_by_email(user_name, password) == true

Sunday, December 4, 2011

HTTP basic authentication in rails to protect staging server

Sometimes, you may need to protect your staging server from outside world. It can be done easily by using http basic authentication in rails3.

I did following things to use HTTP basic authentication to protect my staging server:

YML file with username and password pair:
devuser: devpass
testuser: testuserpass

Loaded YML data from application initializer:
HTTP_AUTH_USERS = YAML.load_file("#{Rails.root.to_s}/config/staging_server_users.yml")

Callback and authentication in the application controller:
before_filter :authenticate_for_staging_server, :if => lambda { Rails.env.development? }

def authenticate_for_staging_server
  authenticate_or_request_with_http_basic do |user_name, password|
    password == HTTP_AUTH_USERS[user_name]

Hope it will help a lot who wants to protect their staging server.

Saturday, November 19, 2011

How to deactivate facebook account

Now a days Facebook became part of our busy life and couldn't think without it. But, if you need to deactivate your account then can do that by following steps below:

Once, you deactivate your account, your profile and information will be instantly inactivated. But, information will not be lost however, and will be available to recover whenever you want within 14 days.

Steps are given below to deactivate the Facebook account:
  • Go to Account menu on top right corner of the page and select Account Settings.
  • Click to Security from right side menu
  • Click to Deactivate your account link from bottom
  • And, then select reason and click to Confirm button
  • System will open a pop-up box for your password and captcha text. Enter those and click on the confirm button.

After confirmation system will redirect to Facebook's home page, where you will see a message confirming that your account has been deactivated.

To cancel deactivation, log-in again and choose cancel deactivation within 14 days.

Monday, October 31, 2011

Rails application deployment using shell script

I have used following shell script to deploy one of my Ruby on Rails applications:
DATES=`date +%Y%m%d%H%I%S`
service apache2 stop &&
mysqldump -u[user] -p[pass] [yourdbname] > "your backup directory"[yourdbname]_${DATES}.sql
git pull &&
bundle install --path [your bundle path] &&
bundle exec rake db:migrate RAILS_ENV=production &&
mv [your application's production log file] [your application's production log backup path]/production_${DATES}.log &&
touch [your application's production log path]/production.log &&
chown [user]:[group] [your application's production log file] &&
service apache2 start
In the above code snippet,
  • Line 01: Used to get the current date time
  • Line 02: Stopped the Apache server
  • Line 03: Used to take database backup in the backup directory
  • Line 04: Getting update source from repository
  • Line 05: Bundle install to update gem list
  • Line 06: Run database migration to migrate latest script if any
  • Line 07: Rotating log in every deployment
  • Line 08: Creating new production log file
  • Line 09: Providing proper user access to write log
  • Line 10: And, then start the server again

That's all. Your application should run without any issue if you configure third bracket's([]) information perfectly.

But, now a days - Capistrano allows you to deploy a Rails applications easily. You can try that and can customize if you need to.

Monday, October 17, 2011

Using helper method from action mailer views

Action Mailer of Ruby on Rails(RoR) allows you to send emails from application using a mailer model and views. Sometimes you may need to use any helper method though it does not allow by default. In that case you have to include helper(module) in the Mailer class.

Say, you may need to use method(s) from ProductsHelper module, then you need to write following line in the Mailer file which will provide accessibility to use all methods written in ProductsHelper


Thursday, September 29, 2011

Disable a user account in Linux

There are some different approaches to do that. The easy way to disable a user account is to alter stored password which is stored to /etc/shadow. In that case password will be lost unless you save save the password in different file.

You can alter the password by using following command:
passwd {username} {new password}

It might better to lock a account rather than change password if you need to re-activate again. By following command a user account can be lock easily:
passwd {username} -l

If you will ever need to re-enable the account just unlock using following command:
passwd {username} -u

If you don't need to keep the account anymore then use following command to delete a user permanently.
userdel {username}

The above operation will keep his home directory, mails, etc.  Use following command to delete all his files on the system.
userdel -r {username}

Be careful to do that, your mistake may destroy all files of the server.

Note: User still will be able to connect using ssh via key-based auth on changing the user password or locking. If this is used, then the user’s authorized_keys must me moved to a different location.

Monday, September 26, 2011

How to resize Facebook UI dialog

Once, I had to re-size the facebook UI dialog while working on friends invitation widget for The widget was loaded under a popup which was wrapped up by an iframe as well. In this case it was not showing properly, the widget was not accommodate in that popup. That's why had to re-size the widget. I did that by following way..
var old_size = FB.UIServer.Methods["apprequests"].size;
FB.UIServer.Methods["apprequests"].size = {
 method: 'apprequests',
 message: 'A nice customizable first page application, visit -'
}, function() { });
FB.UIServer.Methods["apprequests"].size = old_size;
I have stored the old size first and then assign the size which i was needed. After that re-assign the old size again so that it does not broke my another UI dialog. Because in other case i have used default size.


Use following css class to resize the dialog:
.FB_UI_Dialog {
  width: 320px !important;

Saturday, September 24, 2011

Ruby interpreter has stopped working

From last couple of days, I was facing a problem with my ruby interpreter. It was crashing continuously after few page requests and getting message:

Ruby interpreter (CUI) 1.9.2p180 [i386-mingw32] has stopped working

I have tried to find out the reason and frequency to stopped but couldn't find the actual reason. After googling got a suggestion about css media link. But it does not solved the problem as well.

Finally, I have identified that it happens for missing file. The ruby interpreter was crashed after two request for me if the file missing exists, it may differ for others.

Friday, August 19, 2011

LF will be replaced by CRLF

I was getting following message while trying to add files to GIT index:

Warning: LF will be replaced by CRLF ....

By google search I found that - whenever tried to add files to the repository, git thinks that, the file is a text file and all files automatically will have their LF line endings converted to CRLF endings.

It's an important warning for a binary file because git incorrectly assesses a binary file to be a text file but it's OK for text file.

The line-ending conversion can be prevent by setting false of core.autocrlf attribute. By default it remains true.

The command is as follows(in Bash mode):
git config core.autocrlf false

Tuesday, July 26, 2011

Install mysql2 gem on Windows 7

Last couple of days ago, I was stuck to install mysql2 gem on windows7 for rails3 and tried lots of instructions by googling. Unfortunately, those were not worked for me.

Finally, I have specified a older version of mysql2 during installation which worked for me. Following are the steps which i did:
  • Install Mysql 5.5
  • Open command prompt and run following command
    gem install mysql2 --version=0.2.6 --platform=ruby -- '--with-mysql-include="C:\Program Files\MySQL\MySQL Server 5.5\include" --with-mysql-lib="C:\Program Files\MySQL\MySQL Server 5.5\lib"'
  • Copy libmysql.dll from C:\Program Files\MySQL\MySQL Server 5.5\lib to [Ruby Installation Directory]\bin\
  • Specify mysql2 version to you gem file
  • Run bundle install

Friday, June 24, 2011

Unpacked gem has no specification file

I started working on a new rails project and in every time from command line operation getting this type of irritating message:

config.gem: Unpacked gem ..... in vendor/gems has no specification file. Run 'rake gems:refresh_specs' to fix this.

I run rake gems:refresh_specs but the problem isn't fixed. Moreover, tried by creating specification file as well.

After that, removed the irritating message by a single line of code to place in the environment file:

# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')

Rails::VendorGemSourceIndex.silence_spec_warnings = true do |config|

Friday, May 6, 2011

How to disable logging in Rails application

It's not a good practice to do though depends on your needs. It can be done by two ways, one is to disable for whole application and another is to disable for specific environments. You're probably looking for some or all of the following settings:
If you set any of them to nil like following way:
config.active_record.logger = nil
It should disable logging for that specific component. You can experiment with these to disable logging for specific components or set all of them to nil to get no logging.

Friday, April 22, 2011

’rails’ is not recognized as an internal or external command, operable program or batch file

You might experience the problem due to the following reasons:
  • You haven’t installed Rails
  • Rails path cannot be found
  • Installed Rails using a ‘special’ installer

To resolve the issue, uninstall rails installation from your PC and try a normal installation through command prompt by using following command:
gem install rails –include-dependencies

Note: Be sure, you are connected with internet

Friday, March 18, 2011

Remote mysql backups on windows

In our day to day job needs to backup remote mysql database and sometimes may needs to synchronize with another one.

It can be done by using following code snippet, no matter the remote server is windows or Linux.

Save the following code snippet as a batch(.bat) file and setup a task scheduler.

set DATET=%date:~-4%_%date:~7,2%
"your mysql bin directory\mysqldump" --host="remote-host" --user="remote-database-user" --password="remote database password" remote-database-name > "backup-directory"backup_%DATET%_csv.sql
"your mysql bin directory\mysql" -uuser -ppassword -t csv < "backup-directory"backup_%DATET%_csv.sql

Monday, February 14, 2011

How to use mutliple ruby version on windows

I have found a nice ruby version manager for windows named pik. It's pretty easy to install and manage multiple ruby versions.

First install the pik gem:
gem install pik

After installing the pik, you have to use pik_install script to install the pik executable. You must install pik in a different directory other than your ruby\bin directory.
pik_install C:\Windows\System32

If the installation gone smooth, you should see like ..
pik is installed

if you want to use pik with git bash, add the following line to your ~/.bashrc:

  [[ -s $USERPROFILE/.pik/.pikrc ]] && source $USERPROFILE/.pik/.pikrc

Now you can see the list of available ruby versions by using following commands:
pik list

Add, you can install a ruby version and add to version manager using below command:
pik add C:\Ruby192\bin

To switch the ruby version use command:
pik switch [Version Name]

That's all to use multiple version of ruby on windows machine.

Click here to know more about pik

Tuesday, January 11, 2011

‘nmake’ is not recognized as an internal or external command, operable program or batch file

I have experienced the problem due to the version difference of rubygems installed on my developer PC with gems.

I have downgraded the rubygems, to choose the right versions and solved the problem. After completing the installations, updated all of my gems which did not generate any errors.

If you still experience the problem then try the link below to resolve the problem: