Trim white spaces from ruby string


Let’s play with ruby function to remove all white spaces.
1.9.3p125 :001 > a = “Miasa Pride”
 => “Miasa Pride”
1.9.3p125 :002 > a.strip
 => “Miasa Pride”
1.9.3p125 :006 > a.gsub(/\s+/, “”)
 => “MiasaPride”
1.9.3p125 :007 > b = ” Miasa Pride “
 => ” Miasa Pride “
1.9.3p125 :008 > b.gsub(/\s+/, “”)
 => “MiasaPride”
1.9.3p125 :009 > c = ” Miasa@Pride “
 => ” Miasa@Pride “
1.9.3p125 :010 > c.gsub(/\s+/, “”)
 => “Miasa@Pride”
Cheers….!!!!!!!!

 

Advertisements

Create a branch in github


We always used to create a branch on local system and after verify and ok with code, we merged to master branch. Today i was facing situation, i need to create a branch on remote git repository. Actually my manager wanted to take look to my code before merge to master branch(As he is residing out of country).

1.First create the branch locally:

git branch rspec-test-import

2.List all local branch.

git branch
*master
rspec-test-import

3.Checkout to local branch.
git checkout rspec-test-import

4.Again list branch and see where we are?
git branch
master
*rspec-test-import

5.Now create the remote branch and push your code.
git push --all
OR
git push origin rspec-test-import

That's it. Now you able to pull and push your changes to branch repository too. 
Cheers..!!!!!

Install NetBeans IDE on Ubuntu 11.10


1. Make sure, You Added PPA repository to my Ubuntu.

sudo add-apt-repository ppa:ferramroberto/java

2.After adding PPA, update your system apt:

sudo apt-get update

3.Install the Java Development Kit:

sudo apt-get install sun-java6-jdk

If you have problem with java installation , take a look to install-java-and-java-browser-plugin-ubantu-11-10 .

4. Go to the folder where you want to download NetBeans and download by typing:

wget http://download.netbeans.org/netbeans/7.0.1/final/bundles/netbeans-7.0.1-ml-php-linux.sh

5.Give yourself permissions to execute the install script you have just downloaded:

chmod u+x netbeans-7.0.1-ml-php-linux.sh

6. Now run netbeans:

./netbeans-7.0.1-ml-php-linux.sh

Just do next as window, and click desktop icon to start NetBeans:

That’s it. Enjoy……………

twitter bootstrap rails 3 tutorial


Beginning here, we show how to create the application from scratch over over rails 3.2.3 and ruby 1.9.3.

1. Open a terminal, navigate to a folder where you have rights to create files, and Type: Or  do git clone twitter-bootstrap-rails-3-demo  app and get real time experience with TWITTER BOOTSTRAP.

$ rails new twitter-bootstrap-rails-3-demo

2. Set Up Source Control

If you’re creating an app for deployment into production, you’ll want to set up a source control repository at this point. Type:

$ git init
Initialized empty Git repository in /home/pravinmishra/workSpace/twitter-bootstrap-rails-3-demo/.git/
$ git add .
$ git commit -m “first commmit”
[master (root-commit) ffeb58c] first commmit
37 files changed, 1201 insertions(+)
create mode 100644 .gitignore
create mode 100644 Gemfile

3. Create a Home Page.

$ rails g scaffold blog title:string body:text –skip-stylesheets

Note that we’ve used the --skip-stylesheets option in the scaffold as we want to use Bootstrap’s CSS instead of the scaffolding’s. We’ve also migrated the database so that the products table is created.

$ rake db:create
$ rake db:migrate
==  CreateBlogs: migrating ====================================================
— create_table(:blogs)
-> 0.0026s
==  CreateBlogs: migrated (0.0028s) ===========================================

And Delete the default home page from your application:

$rm public/index.html

Next, set a route to your home page. Edit the file config/routes.rb and replace:

root :to => 'blogs#index'

Now it's time to run server and take a look to blog..........

Here’s what the generated scaffold page looks like. It isn’t very pretty as we don’t have any styling applied.

 

4. It’s time to add Twitter Bootstrap. Add twitter bootstrap gem in gemfile and do bundle update.

gem “twitter-bootstrap-rails”
$ bundle update
Using sqlite3 (1.3.6)
Using therubyracer (0.10.1)
Using twitter-bootstrap-rails (2.0.7)
Using uglifier (1.2.4)

And we are saw latest version of bootstrap 2.0.7 is install on application.

Now we need to generate some file by using command

$ rails g bootstrap:install
insert  app/assets/javascripts/application.js
create  app/assets/javascripts/bootstrap.js.coffee
create  app/assets/stylesheets/bootstrap_and_overrides.css.less
gsub  app/assets/stylesheets/application.css
gsub  app/assets/stylesheets/application.css

As we saw it create some css and javascript file for us in assets folder.  One key file that was generated is bootstrap_and_overrides.css.less. This file loads up Bootstrap and it’s a good place to customize the styling to suit your application. After we restart the server and reload the page we can see that the styling is already starting to look better, but there’s still a lot to do.

 

 

5. Customize your css.

As we see button having default css we can make it more decent adding bootstrap class properties. Here we adding <%= f.submit :class => “btn btn-primary” %> , class to submit button in page views/blog/_form.html.rb file. Now once more take a look to browser.

Button style has changed as expected. The is a lot more css and javascript features of twitter boostrap. Once take a look and play with it.

That’s it. Enjoy………..!!!!!!!!!!!!!!!!!!!!!

PostgreSQL installation in Ubuntu 12.04


PostgreSQL is a powerful and reliable object-relational database system. It’s a great alternative for MySQL. It is as easy to set up, performs better and offers far more features.

1. Make sure you already have install python-software-properties

$sudo apt-get install python-software-properties

2.Add PPA repository to my Ubuntu.

$sudo add-apt-repository ppa:pitti/postgresql

3. After adding PPA, update your system apt:

$sudo apt-get update

4.Finally install postgresql-9.1:

$sudo apt-get install postgresql

if you having any error, make sure you already install libpq-dev.The libpq-dev package is for compiling wrappers/clients against libpq.

$sudo apt-get install postgresql-9.1 libpq-dev

5.Now check it out installation is successful or…..

$ locate postgresql

6. If done!!!

Check the install version.

$psql -V

7.Now let’s take look to postgres console

$su postgres

7(a) Setup Root User ‘posrgres’

$sudo passwd postgres

give the postgres user a (unix) password,Now we can switch to the user postgres using command

$ su postgres

8. That’s it for postgers installation. If you want to access postgres from remote, Mean localhost. You need to perform some addition setting to postgres conf file.

First we edit the file postgresql.conf:

$ cd /etc/postgresql/9.1/main

List all file in main folder

$ ls -la

total 80
drwxr-xr-x 2 postgres postgres 4096 2012-05-17 18:05 .
drwxr-xr-x 3 postgres postgres 4096 2012-04-07 12:44 ..
-rw-r–r– 1 postgres postgres 316 2012-04-07 12:44 environment
-rw-r–r– 1 postgres postgres 143 2012-04-07 12:44 pg_ctl.conf
-rw-r—– 1 postgres postgres 4653 2012-05-17 18:05 pg_hba.conf
-rw-r—– 1 postgres postgres 4649 2012-04-09 19:29 pg_hba.conf.bak.original
-rw-r—– 1 postgres postgres 1636 2012-04-07 12:44 pg_ident.conf
-rw-r–r– 1 postgres postgres 19235 2012-04-07 12:44 postgresql.conf
-rw-r–r– 1 postgres postgres 19235 2012-04-09 19:29 postgresql.conf.bak.original
-rw-r–r– 1 postgres postgres 378 2012-04-07 12:44 start.conf

I have already back of config file, you need to make back up for further need

$ cp pg_hba.conf pg_hba.conf.bak.original
$ cp postgresql.conf postgresql.conf.bak.original

9. Make changes to pg_hba.conf (authetification methods).

host    all         all       127.0.0.1/32       trust          # md5 -> trust

10.You make change in config file, So once restart you postgres

$sudo /etc/init.d/postgresql restart
*Restarting PostgreSQL 9.0 database server
That’s it. Enjoy………

Devise Authentication in Rails 3


Hi folk, We are writing step by step devise authentication for a sample app over rails 3.2.3 and ruby 1.9.3.

 

To create the application, you can cut and paste the code from the blog into your console ELSE do git clone rails_3_devise app and get real time experience with device. 


1. Creates a new Rails app named rails_3_devise on your computer. Type the below command.

 

$ rails new rails_3_devise –d mysql

 

Use the flag  –d mysql specify mysql as database, As we know rails having default sqlite db.

Now we move to app by typing 


$ cd rails_3_devise –d mysql

 

2. Finally we have to push our code to github, So doing some basic cammand to initialize git and pushing code to github  .
$ git init
Initialized empty Git repository in /home/maisa/workspace/rails_3_devise/.git/
$ git add .
$ git commit -m “First commint” 

 

3. Once we make sure our app is working

 

 

$ rails s -p 4000 

 

And check from browser http://localhost:4000/

 

4. Set Up Authentication, First things first, add “devise” to your rails Gemfile in the root directory of your application And do

 

$ bundle update

Fetching gem metadata from https://rubygems.org/……&#8230;

Using warden (1.1.1)

Using devise (2.1.0)

Using jquery-rails (2.0.2)

Using rails (3.2.3)

Using sass (3.1.18)

 

We will see latest devie 2.1.0 has installed

 

Now following the install docs on the Devise project page. And run

 

$ rails g devise:install

create  config/initializers/devise.rb

create  config/locales/devise.en.yml

===============================================================================

 

Some setup you must do manually if you haven’t yet:

 

1. Ensure you have defined default url options in your environments files. Here

is an example of default_url_options appropriate for a development environment

in config/environments/development.rb:

 

config.action_mailer.default_url_options = { :host => ‘localhost:3000’ }

 

In production, :host should be set to the actual host of your application.

 

2. Ensure you have defined root_url to *something* in your config/routes.rb.

For example:

 

root :to => “home#index”

 

3. Ensure you have flash messages in app/views/layouts/application.html.erb.

For example:

 

<p><%= notice %></p>

<p><%= alert %></p>

 

4. If you are deploying Rails 3.1 on Heroku, you may want to set:

 

config.assets.initialize_on_precompile = false

 

On config/application.rb forcing your application to not access the DB

or load models when precompiling your assets.

 

===============================================================================

 

Alright, based on the install output it looks like we need to do a little setup work… Make sure we are following all above instruction.

After you are finished configuring the application its time to create your user model.

$ rails generate devise User

invoke  active_record

create    db/migrate/20120516110556_devise_create_users.rb

create    app/models/user.rb

invoke    test_unit

create      test/unit/user_test.rb

create      test/fixtures/users.yml

insert    app/models/user.rb

route  devise_for :users

 

Great, lets take a look at what the generator created starting with the migration.

 

class DeviseCreateUsers < ActiveRecord::Migration

def change

create_table(:users) do |t|

## Database authenticatable

t.string :email,              :null => false, :default => “”

t.string :encrypted_password, :null => false, :default => “”

 

## Recoverable

t.string   :reset_password_token

t.datetime :reset_password_sent_at

 

## Rememberable

t.datetime :remember_created_at

 

## Trackable

t.integer  :sign_in_count, :default => 0

t.datetime :current_sign_in_at

t.datetime :last_sign_in_at

t.string   :current_sign_in_ip

t.string   :last_sign_in_ip

 

## Confirmable

# t.string   :confirmation_token

# t.datetime :confirmed_at

# t.datetime :confirmation_sent_at

# t.string   :unconfirmed_email # Only if using reconfirmable

 

## Lockable

# t.integer  :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts

# t.string   :unlock_token # Only if unlock strategy is :email or :both

# t.datetime :locked_at

 

## Token authenticatable

# t.string :authentication_token

 

 

t.timestamps

end

 

add_index :users, :email,                :unique => true

add_index :users, :reset_password_token, :unique => true

# add_index :users, :confirmation_token,   :unique => true

# add_index :users, :unlock_token,         :unique => true

# add_index :users, :authentication_token, :unique => true

end

end

 

Now let’s take a look at our User model.


class User < ActiveRecord::Base

# Include default devise modules. Others available are:

# :token_authenticatable, :confirmable,

# :lockable, :timeoutable and :omniauthable

devise :database_authenticatable, :registerable,

:recoverable, :rememberable, :trackable, :validatable

 

# Setup accessible (or protected) attributes for your model

attr_accessible :email, :password, :password_confirmation, :remember_me

# attr_accessible :title, :body

end

 

We don’t need to setup our routes for user because devise has already added devise_for :users to our config/routes.rb for us. If we then run rake routes from the command line we get:

 

$ rake routes user
              home_index GET    /home/index(.:format)          home#index
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
                    root        /                              home#index

Finally it’s time to migrate our database and create our user model.

$ rake db:migrate

==  DeviseCreateUsers: migrating ==============================================

— create_table(:users)

-> 0.0175s

— add_index(:users, :email, {:unique=>true})

-> 0.0010s

— add_index(:users, :reset_password_token, {:unique=>true})

-> 0.0012s

==  DeviseCreateUsers: migrated (0.0202s) =====================================

 

5. Now let’s create a Home Page and add custom message to index page

 

$ rails generate controller home index –no-controller-specs      create  app/controllers/home_controller.rb

route  get “home/index”

invoke  erb

create    app/views/home

create    app/views/home/index.html.erb

invoke  test_unit

create    test/functional/home_controller_test.rb

invoke  helper

create    app/helpers/home_helper.rb

invoke    test_unit

create      test/unit/helpers/home_helper_test.rb

invoke  assets

invoke    coffee

create      app/assets/javascripts/home.js.coffee

invoke    scss

create      app/assets/stylesheets/home.css.scss

 

And remove default public index page, As we expecting home/index will be our default page.

 

$ rm public/index.html

And will add root url to routes.rb file  and protect user controller adding  

 

before_filter :authenticate_user!

 

At top of index action.

 

root :to => “home#index”

 

 

 

Now  we almost done. Finally we need to create  an Application Layout with few login, logout,sign links. For that we Create a shared directory under app/views/. Then create the file app/views/shared/_navigation.html.erb and add:

 

<% if user_signed_in? %>

<li>

<%= link_to(‘Logout’, destroy_user_session_path, :method=>’delete’) %>

</li>

<% else %>

<li>

<%= link_to(‘Login’, new_user_session_path)  %>

</li>

<% end %>

<% if user_signed_in? %>

<li>

<%= link_to(‘Edit account’, edit_user_registration_path) %>

</li>

<% else %>

<li>

<%= link_to(‘Sign up’, new_user_registration_path)  %>

</li>

<% end %>

 

Then use these partials in your app/views/layouts/application.html.erb file, like this:

 

<ul>
<%= render ‘shared/navigation’ %>
</ul>
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => “flash_#{name}” if msg.is_a?(String) %>
<% end %>
<%= yield %>

 

Now Add stylesheet rules to the application.css.scss file:

 

header nav ul {
list-style: none;
margin: 0 0 2em;
padding: 0;
}
header nav ul li {
display: inline;
}
#flash_notice, #flash_alert {
padding: 5px 8px;
margin: 10px 0;
}
#flash_notice {
background-color: #CFC;
border: solid 1px #6C6;
}
#flash_alert {
background-color: #FCC;
border: solid 1px #C66;
}

 

That all coding part.  

 

Now it’s time to start our rails server and see what devise has given us. Run rails server from the command line and lets travel to “localhost:4000/

 

That’s expected. As we protected our home page. Now we need to sign_up and post sign_in. After using right credentials we will see

 

 

 

 

 

Thanks, Enjoy……………….