How to install devise in rails for user authentication

From the docs
Devise is a flexible authentication solution for Rails based on Warden. It:

  • Is Rack based;
  • Is a complete MVC solution based on Rails engines;
  • Allows you to have multiple roles (or models/scopes) signed in at the same time;
  • Is based on a modularity concept: use just what you really need.

Now let us start with some real stuff πŸ™‚

Create new rails project by running the following command –

rails new devise_install

Now open your GEM file (located at the base root of your folder, look for Gemfile in your app)

Add these two lines in your gemfile –

gem 'devise'

gem 'omniauth'

Now navigate to your app folder and run following command –

bundle install

Un comment bcrypt-ruby in gem file to use Active mode has_secure_password

Once you are done, run the following command –

rails generate devise:install

This command will generate an initializr to describe all devise configuration options
You MUST read those points before proceeding further.

Now we are ready to use it with MODEL.

Run the following command –

rails generate devise User

It will create a User model if not exists previously.

You will see some new files generated after the command. To this point I am interested in two files only one is under db/migrate folder i.e 20130321042152_devise_create_users.rb (in my case) and another one in app/model i.e user.rb

Now change 20130321042152_devise_create_users.rb to something like this –

And your user.rb file will go something like this –

Finally run this command –

rake db:migrate

Once all done go to http://localhost:3000/users/sign_in and you will see the devise magic πŸ™‚

NOTE – Do not forget to START your server and if you are facing some trouble with rail routes un comment the following line in your routes.rb file

match ':controller(/:action(/:id))(.:format)'

If you still not clear with the file structure you can PULL the code from github

Program to find prime numbers using PHP

This is a simple program in PHP to find prime numbers using for loop and ifΒ  statement.

<?php
error_reporting(E_ALL);
//Program to find prime numbers in between a range

$num =23;

for( $j = 2; $j <= $num; $j++ )
{
for( $k = 2; $k < $j; $k++ )
{
if( $j % $k == 0 )
{
break;
}

}
if( $k == $j )
echo “Prime Number : “, $j, “<br>”;
}

OUTPUT

Prime Number : 2
Prime Number : 3
Prime Number : 5
Prime Number : 7
Prime Number : 11
Prime Number : 13
Prime Number : 17
Prime Number : 19
Prime Number : 23

 

Posted in PHP