Development

A Simple Twitter App with Ruby on Rails – User Authentication

July 16th, 2009

This article is the second part in a three part series. In the first part we created some basic functionality to allow the user to post messages in a similar way to Twitter. If you have not completed the first part of this tutorial, please do so now.

In this instalment, we will learn how to add some basic user authentication using a useful plugin called nifty_generators. So lets get started!

Get the Gem!

nifty_generators Gem

The nifty_generators gem was created by Ryan Bates, better known for his great RailsCast tutorials. It provides a useful generator for setting up user authentication in a few simple steps.

It’s worth noting that there are other tools for integrating authentication, for instance technoweenie’s Restful Authentication is very good. However, to keep things simple, we will stick with nifty_generators.

At a command prompt key the following command:-

  > gem install nifty-generators

Generating the Authentication

Authentication

Now, make sure your current directory is set to the project folder of your Rails project and run the following commands:-

  > ruby script/generate nifty_authentication
  > ruby script/generate nifty_layout

The first command creates the relevant files for achieving authentication. The second command creates, among other things, a helper file which is required for some of the authentication view files to function correctly.

Don’t forget to migrate your database!

  > rake db:migrate

Making use of the Authentication Module

Authentication

Ok, so we now have the functionality, which is required for authenticating users, it’s just a matter of using it. First, we will create a partial allowing the user to sign up or login.

Create a file called “_login.html.erb” in the views/users folder and add the following code:

< % if logged_in? %>
  Welcome < %= current_user.username %>! Not you?
  < %= link_to "Log out", logout_path %>
< % else %>
  < %= link_to "Sign up", signup_path %> or
  < %= link_to "log in", login_path %>.

< % end %>

We will simply render the login partial in the “posts.html.erb” layout file (to keep things simple). This file is located in the views/layouts folder. Edit the file to include the “render” method call, as shown below.

.
.
<body>
  <div id="content">
    <p><%= render :partial => "users/login" %></p>

    <%= yield %>
  </div>
</body>
.
.

Further to this, we only want users to be able to post messages if they are logged in so change the “_message_form.html.erb” file, in the views/posts folder, to have the following condition:

<% if logged_in? %>

  <% form_remote_tag(:controller => "posts", :action => "create") do %>
    <%= label_tag(:message, "What are you doing?") %><br />
    <%= text_area_tag(:message, nil, :size => "60x2") %><br /> 
    <%= submit_tag("Update") %>

  <% end %>
<% end %>

Let’s Give it a Go!

Fire up the server.

  > ruby script/server

On the home page we now have “Sign up” and “Login” links.

Sign Up and Login Links

You’ll also notice that you cannot post a message until you have a user account. So you will need to sign up first

Sign Up Form

Then you can login…

Our Twitter login page

Once you have logged in, you will be able to post messages again.

Logged in to our Twitter

View Demo of Twitter App with Ruby on Rails

Summary

In this tutorial, you have leanrt how to integrate authentication into your applications and how to make use of it. In the third and final part of this series you will learn how to add functionality, which allows users to follow other users.

Author: Phil McClure

Phil McClure is a Software Developer from Belfast, Northern Ireland. His main interests are software architecture, design patterns and how these can be applied to web development. Phil blogs at Therailworld. Follow him on Twitter.

Write for Us! We are looking for exciting and creative articles, if you want to contribute, just send us an email.

The jungle is alive: Be it a collaboration between two or more authors or an article by an author not contributing regularly. In these cases you find the Noupe Editorial Team as the ones who made it. Guest authors get their own little bio boxes below the article, so watch out for these.

Tags: ,

33 comments for „A Simple Twitter App with Ruby on Rails – User Authentication
  1. Nachtmeister on July 16th, 2009 at 5:49 am

    Looks good. Thanks for it!

  2. trice22 on July 16th, 2009 at 11:08 am

    The headline is slightly misleading: This is rather a Twitter–like application.
    Reading this headline I had expected to find a tutorial on how to make use of the Twitter API, using RoR.

    Regards,
    trice

  3. insic on July 16th, 2009 at 12:07 pm

    nice tut about user authentication in RoR. but I was expecting you are using the twitter API. :) this is not a twitter APP :)

  4. Ann Arbor Michigan Website Promotion on July 16th, 2009 at 3:23 pm

    That’s a neat little application isn’t it?

  5. joyoge designers' bookmark on July 16th, 2009 at 7:19 pm

    nice tut of ruby & twit, thanks for post..

  6. Premium Theme Info on July 16th, 2009 at 10:19 pm

    Nice tutorial, it’s useful to me. I think it also should be useful to many readers!

  7. Gizelle on July 17th, 2009 at 2:50 pm

    You are a genius!! This is very useful for someone like me who’s a poor coder.

  8. dupedb on July 19th, 2009 at 6:45 am

    Nice thanks. Was thinking about learning ruby a while back.

    (first comment on this site btw(for me)) :>

  9. GIm on July 20th, 2009 at 3:18 am

    Awesome one dude. Waiting for the third part :)

  10. ntiremedia on July 20th, 2009 at 10:08 pm

    i am glad that i bumped in to you ppl i like the work here

  11. shaunjudy on July 28th, 2009 at 7:14 pm

    This is a cool trick. Is more to come?

  12. clark on July 29th, 2009 at 5:23 pm

    another great article form noupe, I am learning ruby on rails right now, this is great help

  13. maheshbalaji on August 1st, 2009 at 4:43 pm

    its a great one im waiting for the next one

  14. Alex on August 9th, 2009 at 9:57 pm

    Thanks a lot! Ruby on rails rocks!

  15. Niler Barcelos on September 9th, 2009 at 5:59 am

    great tutorial!!!

    thanks!!!!!!!

  16. subzcat on November 30th, 2009 at 4:46 pm

    Thanks so much for this!

  17. DerNalia on March 30th, 2010 at 9:12 am

    the _login form doesn’t have the code in the tutorial =/

  18. vineeth kumar.s on April 26th, 2010 at 2:59 am

    Excellent tutorial!!!
    Rock on!!!

  19. Selvam on May 3rd, 2010 at 4:25 pm

    Helpful tutorial. But seems to miss couple of pieces here and there – e.x. the _login form does not work as is.

  20. Selvam on May 3rd, 2010 at 4:38 pm

    Below is the _login form.

    Welcome ! Not you?

    or
    .

    • Selvam on May 3rd, 2010 at 4:41 pm

      Now I see why the author could not post it properly :-) Read the following code using the legend.

      lt = less than symbol
      pt = percentage symbol

      ltpt if logged_in? ptlt
      Welcome ltpt= current_user.username ptlt! Not you?
      ltpt= link_to “Log out”, logout_path ptlt
      ltpt else ptlt
      ltpt= link_to “Sign up”, signup_path ptlt or
      ltpt= link_to “log in”, login_path ptlt.
      ltpt end ptlt

      • Chim Kan on June 8th, 2010 at 5:57 pm

        Thanks a lot! The code was really messed up but thanks Selvam!

  21. rubynoob on September 20th, 2010 at 6:23 am

    For some reason I keep getting this message everytime I run the server…

    Routing Error

    uninitialized constant ApplicationController::Authentication

    Can anyone point me in the right direction so that I can continue this awesome tutorial!?!

    • rubynoob on September 20th, 2010 at 6:27 am

      from my terminal (using ubuntu 9.04)

      ActionController::RoutingError (uninitialized constant ApplicationController::Authentication):
      app/controllers/application_controller.rb:2
      app/controllers/posts_controller.rb:1

      Rendered /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.8ms)

  22. Judy Modafferi on November 30th, 2010 at 1:42 pm

    Great blog. I have been wearing glasses for more than 10 years and have just recently discovered the eye exercise program by dr. Bates. The results are great!

  23. Barwon on January 18th, 2011 at 7:52 am

    I was suggested this blog by my cousin. I’m not sure whether this post is written by him as nobody else know such detailed about my difficulty. You are incredible! Thanks!

  24. SeedsWeed on January 18th, 2011 at 10:25 am

    It is appropriate time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I wish to suggest you some interesting things or advice. Maybe you can write next articles referring to this article. I desire to read more things about it!

  25. veedles on April 20th, 2011 at 5:03 pm

    thanks Phil – really nice tutorial :)

  26. madhavi on July 4th, 2011 at 11:59 pm

    Does the create action under session controller does not seem to be working. When I enter wrong username/password, it still redirects me the url localhost:3000/sessions. Did I do something wrong?

  27. Markow on October 14th, 2011 at 7:49 am

    For some reason, I ended up with my posts.html.erb file in views/posts and it needs to be in view/layouts. Clearly stated in the tutorial, I’m just confused with how it got there.

  28. treewhisperer on October 27th, 2011 at 8:59 pm

    Darnit I’m stuck can’t get the nifty_generators to install correctly, I’m on windows7. To bad I can’t finish this tutorial. :(

  29. Disha on December 14th, 2011 at 11:22 am

    With Rails3 on nifty-generators now work with a ” : ” instead of ” _ “.
    So, nifty_layout becomes nifty:layout,
    so does with nifty:authentication and nifty:scaffold.

  30. John on October 19th, 2012 at 5:45 pm

    For Rails 3 after you install the nifty-generators, you have to edit the Gemfile adding:
    gem “nifty-generators”, :group => :development

    make sure you “gem install mocha” as well and add this line as well
    “gem “bcrypt-ruby”, :require => “bcrypt””

    Also as Disha said, do “rails g nifty:authentication instead of nift_authentication. Same goes for layout.

    oh, and don’t forget to restart the server! Hope this helps :)