Map Remote Server Directories to Local Ubuntu Machine

At times we need to perform a number of actions on directories of remote server. For that either we use ssh or we do scp. Another good alternate is mapping the remote server directory to our local machine. Lets get started with

sudo apt-get install sshfs
Read on →

Using IP Tables to Setup Firewall on Ubuntu Servers

By Default a new server on Digital Ocean or any other provider accepts incoming requests from every IP and of all types. We can filter the request types or whitelist IP to accept requeest.

On Terminal

sudo iptables -L

gives details about rules defined in IP Table

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

It clearly shows that no rules are defined by default. Now we start adding rules to the IP Table. To add a rule

    sudo /sbin/iptables -A INPUT -i eth0 -s 10.5.1.3  -p tcp --destination-port 3306 -j ACCEPT

    sudo /sbin/iptables -I INPUT 2 -i eth0 -p tcp --destination-port 80 -j ACCEPT
Read on →

Using Developer Specific Gems on Local Machine

Sometimes developer has different preferences for Gems to be used in Development Enviorment. Some developer may want to user thin as server, some body else may use default webbrick.

We also dont want to do a check everytime if we are committing the Gemfile with our Gems, or it is a Universal Gemset, which every one is using.

Simplest solution is create a new file named Gemfile_Custom. Add this to you main Gemfile as :

Gemfile
1
2
3
4
5
6
7
8
9
10
11
source 'https://rubygems.org'

gem 'rails', '4.0.0'
gem 'mysql2'
...
...
...

if File.exists?('Gemfile_Custom') then
  eval File.read('Gemfile_Custom'), nil, 'Gemfile_Custom'
end    

Last 3 lines are most important. They include the Gems from Gemfile_Custom to your main Gemfile. Now add whichever Gems you want to use to Gemfile_Custom.

Gemfile_Custom
1
2
3
4
5
6
7
source 'https://rubygems.org'


gem 'quiet_assets'
gem 'thin'
gem 'annotate'
gem "foreman", "~> 0.63.0"

Dont’t forget to add Gemfile_Custom to .gitignore. That ways whichever Gem you mention in Gemfile_Custom all are available only to your machine only.

Also, make sure you don’t add any such gem to Gemfile_Custom which is a must required as a dependency to run the application.

Find and Kill Process Locking 3000 Port

Sometimes, after a crash or some bug or sometime by mistake we forcefully shut down the rails server using Ctrl + Z. After that when we try again to run rail s it gives some error like

Address already in use - bind(2) (Errno::EADDRINUSE)

We can kill the process blocking the port 3000 simply by putting the following code in terminal.

kill -9 $(lsof -i tcp:3000 -t)

Also, we can use (doesn’t work wih MAC)

fuser -k 3000/tcp

We can also create alias to ‘k3000’ in ~/.bash_aliases to kill server in case we don’t remember the command. Happy Coding :)

Saving All Uploaded Attachments With Paperclip

Paperclip is available as an excellent gem to get started with image or attachments upload. It helps you get kick started with in a few minutes.

Include gem

gem 'papeclip'

Run bundle

bundle install

Add to your model

user.rb
1
2
3
4
5
6
7
8
class User < ActiveRecord::Base

  has_attached_file :attachment,
  :url => :attachment_url,
  :path => :attachment_path,
  :default_url => "/images/sheet.png"

end    
Read on →

A/Bingo - Nice Plugin for A/B Testing the User Response

A/Bingo is a Ruby on Rails A/B testing framework written as a plugin. It can be used to toggle between various options and get stats based upon user interaction.

Install A/Bingo

script/plugin install git://git.bingocardcreator.com/abingo.git

Create Migration

script/generate abingo_migration

Migrate database

rake db:migrate
Read on →

Fetch Current Branch and Latest Commit_ID in Rails Application

Sometimes while running project on a staging server, we can are required to check the current branch and latest commit for the code. Simply create a new Initializer file git_info.rb

git_info.rb
1
2
GIT_BRANCH = `git rev-parse --abbrev-ref HEAD` || 'none'
GIT_COMMIT = `git log | sed -n 1p`.gsub(/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/,'').split.last || '0'   

and now you can use these GIT_BRANCH and GIT_COMMIT anywhere in your views and as per requirement.

git

Changing the Git Commit Messages

Git commits are self explanatory that what all code has been done and what all features has been implemented with current commit. But some times we miss out one or two things to mention in our commit. There is a pretty clean and simple way to change the comment for latest commit :

git commit --amend -m "my new message with more information"

Easy and Simple :)

git

Setting Custom URL and Paths for Paperlip Attachments in Rails

PaperClip is an awesome file attachment library for Active Record built by thoughtbot. It helps you get started with uploads in rails in less than a few minutes.

Read on →

Getting Started With FireFox Plugin

Last week I was building up the FireFox Extension to support our application on FireFox Browser. Here are the few steps to get started :