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

Advertisements

How to create Ruby on Rails Application

  • I am developing RoR applications on Windows 7.
  • Assuming that you installed Rails framework on your system before creating application.

Type rails as shown in screenshot and click on command prompt for Rails framework.

Now being a php developer i already have XAMPP installed on my machine and i want my projects to come under the htdocs.
I created a new directory in htdocs as railrepo to store my rail projects. I am naming my first project as project1.

Check this screenshot for directory structure –

Now under railrepo directory I am creating my First rail project(project1) using new command.

rails new project1

Type this command under your railrepo folder as shown in sceenshot-

Once you are done with files creation, you are expecting this to show on your browser. For this you need to start rails server first. Type this command in the folder you created your application by opening another command prompt window.

Command – rails server

Check the screenshot –

Once your rails server started check the action in your browser, by typing —

localhost:3000

Check the screenshot as well–