Why Making Web Apps with Rails Is Awesome

Mar 25 2011 by Marc Gayle | 36 Comments

Why Making Web Apps with Rails Is Awesome

I’m fairly new to the Ruby on Rails web application framework. I just started learning it about eight months ago when I started developing my web app.

I’ll share a story with you about my experience with Rails, and maybe you’ll be intrigued enough to try it out for yourself.

About My Web App

CompVersions is a web application that allows designers to upload multiple versions of their designs so that their clients can easily give them a thumbs up or a thumbs down as well as provide comments as necessary.

Here are some images of the app:

The idea for the app comes from my previous experiences working with multiple designers. I have, at various points in the past, worked on projects where I was either project lead or project manager and I had to coordinate designs and feedback from multiple designers and stakeholders.

Getting and forwarding emails with PDF files as attachments and notes specifying the line and page number in the PDF was not cutting it anymore.

I tried the popular project management app, Basecamp (also built on top of Rails, by the way), but it wasn’t designed for this problem, so it didn’t work the way I needed it to.

I decided to build my own solution.

A friend of mine (Hector) is on a similar journey (building a web app/site while still learning about the framework), but he is in "Microsoft Land" — .Net, IIS servers, MSDN — the whole nine yards.

I was explaining to him why I love Ruby on Rails (RoR for short), and how it completely abstracts the pain of doing things like database queries (although, if you want to work with them, you surely can).

I was trying to find a real-world example of what I meant, but all I found were the regular "build a blog in 15 minutes" tutorials and articles that take you through the entire app development process.

I didn’t want that, so I started looking through my code and realized that I have the perfect use case to highlight how wonderful it is to use Rails.

He appreciated the example, and understood what I meant by way of a practical, real-world example that’s actually in use.

I figured I would share to you that example I showed Hector.

Rails Business Logic

Rails is known for allowing the developer to focus more on the business logic of the application, rather than the technical, behind-the-scenes stuff.

What does that even mean? Here is an example: I was hooking up the email functionality for CompVersions so that once a designer registers, they get an email from the app.

I used a Rails gem (similar to what a plugin is for WordPress or a prewritten class in PHP) called Devise that makes it relatively easy to get up and running with an authentication solution. It handles users logging in and out: Registering, forgotten passwords, resetting passwords, and so forth. Locking and unlocking an account — say if your application requires high security where you only allow a certain number of login attempts — was baked right in, no need to code it. It had a bunch of other awesome features that solve everyday web app development problems.

That’s right, you get all that functionality from one Rails gem. All done to industry standard design patterns, e.g., when a user forgets their password, a unique link is sent where they can reset their password.

So the first question I was faced with was, How do I format the email? Not, How do I programmatically tackle something like this? or Can I write code to send out emails?

Do I say "Hi John!"? But what if there is no first name, how do I fallback to their username (sexyjohn79) and then subsequently their email (sexyjohn79@email.com)?

That’s business logic.

Writing the Business Logic

The registration form for CompVersions has input fields for first name, last name, username and email address. A user can log in with either their username or email address. But I haven’t decided whether or not I’m going to let first and last names be required form fields. Most likely, I will, but I wanted to implement it to gracefully handle the situations when a user doesn’t provide one or the other.

Rails uses what’s called an MVC structure. Model, View, Controller. Rails encourages you to put all of your business logic in the Model portion of your app.

In my User model (which is where Rails handles all the business logic related to the users of the app), I created a method called pretty_name that handles the business logic.

This is how my pretty_name method looks:

def pretty_name
  f_name || username || email
end

The first line is the definition of the method. The method is what I will invoke to print out the designer’s name in the email.

The method translated in plain English: "If the value f_name exists, return that value, if not, try username, and if that doesn’t exist, then try email". The syntax will be familiar to many of you: || means OR.

The assumption here is that a username must exist because the user will be getting this message through email. Ruby executes left-to-right.

That’s it. That’s the business logic.

Applying the Business Logic to the View

In my template for the actual email — the View in MVC — this is what the first few lines look like:

<p>Hi <%= @resource.pretty_name %>!</p>
<p>Thank you for registering with CompVersions.</p>

The template prints ‘Hi ‘ and then it invokes the pretty_name method on the object called @resource. @resource is an object that is used to refer to the current user account and then returns the best value.

So if the person’s first name is John and it’s available (i.e., the value is not nil), the email will say ‘Hi John’.

If there is no first name, it will use the username (e.g., ‘Hi sexyjohn79′).

If there is no username, it will say something like ‘Hi sexyjohn79@email.com’, falling back to the user’s email address.

The Result

Here is an example of how the email looks.

Some Parting Thoughts

So is Rails really a walk in the park for non-developers?

I don’t want you to misinterpret what I’ve said. I’m not implying that getting from nothing to a full SaaS web app with no Ruby and/or Rails knowledge is effortless. It’s not that easy.

But as a general rule when using Rails, the things you will be struggling with (once you get the basics down) are related to how the app will function (business logic) rather than the minutia, set up, coding syntax, and the writing of common web app methods and functions. The focus will be on the logic of the app rather than missing semi-colons, setting up a code project, and errant SQL queries.

Like all web development frameworks, Rails has its quirks. Rails has a set of conventions that make sense and force you to develop a certain way, which makes it especially tricky for those immutably set in their processes.

I hope this helps encourage you to try Rails and to consider it for your next web app project. There are many awesome tutorials out there. For starters, I would recommend Rails for Zombies.

Related Content

About the Author

Marc Gayle is a Rails developer and founder of 5KMVP, where he builds Minimum Viable Products for just $5K. Follow him on Twitter and check out his personal blog.

36 Comments

Paul Sprangers

March 25th, 2011

I love Ruby on Rails and have been messing around with it for some time now. Unfortunately I haven’t gotten around to create an actual app with it. My company uses PHP primarily and I’m doing probably the least programming.

Stil I love the framework and reading stuff like this always gets me pumped to dive into it a little deeper. And I learned something new! Didn’t know about the syntax in your pretty_name method. Using the ‘cascading or’ (is that the correct term?) construct. Great stuff in Ruby and in Rails!

Filip

March 25th, 2011

It doesn’t scale! ;-) Just kidding, but somebody had to say it.

Sherzod Shermukhamedov

March 25th, 2011

Before learning ZendFramework, I had tried to learn Ruby but it hasn’t token me a development mood.
Code syntax is so strange! Maybe Ruby developers have invented original syntax.
In my opinion PHP more easily understanding language.

Jeff Cohan

March 25th, 2011

Thank you for that. I really like the way you wrote this article; extremely readable. It just might persuade this confirmed (but not immutable) php-from-scratch developer to give another look at RoR.

Jeff

Neil H

March 25th, 2011

It will never be awesome. Please learn the meaning of ‘awesome’.

Alex Ramadan

March 25th, 2011

Thanks for this. I have just started messing around with Ruby (not onto Rails just yet)with absolutely zero programming experience and it is amazing how much sense the language makes to those who aren’t familiar with programming.

Bookmarking this article for some inspiration when I finally make my way onto ROR.

Ash Menon

March 25th, 2011

Good call, Marc, this sounds like something I could really use at this point in time (I’m developing my own web app, currently in PHP, about 30% in). It may be a tad too late for this project, but I’ll definitely keep Rails in mind for future ideas.

Cheers.

Marc Gayle

March 25th, 2011

Paul,
Thanks for the feedback. I know the feeling of just messing around. If you want to dive in, I would recommend choosing a project that you are passionate about and trying to create it. Nothing teaches like experience – I have found.

Btw, I think I learned something too. I am not sure if it is called ‘cascading or’, but the name makes sense.

Thanks :)

Tom

March 25th, 2011

Hey Marc,

Great article. Without giving away your process or code, I think a great article/tut/video would be the process of creating a login, username, etc. Basically an admin section.

Even further would be an explanation of how those pages are secured and/or not secure.

I’m going to be taking on RoR soon, so this processes will be most valuable.

I appreciate you taking the time to write it!

Josh Tintner

March 25th, 2011

Wow, I was just telling my buddy how the other day how I was trying to find some article(s) which talked about real world implementation of RoR.

I have some coding background in HTML CSS and JavaScript and have some ideas of what I would like to do with RoR but have never really implement them or have found a good resource. Every time I go and try and find one I get lost frustrated but this has given me the re-found inspiration. Thanks for the encouraging words that it CAN be done!

Marc Gayle

March 25th, 2011

@Filip That’s good.

I appreciate the support & positive feedback from you guys.

@Tom thanks for the request. I appreciate the suggestion for the new post idea. I am not sure if/when I will be able to do that – but follow me on Twitter to find out when I do ;)

Jonathan BĂșcaro

March 25th, 2011

It looks like a promising app :)

Dayson

March 25th, 2011

I find the code syntax very very strange. I still believe php has a much easier learning curve. It’s only the way conventions are enforced is what I appreciate about rails. Besides that, I’ve never found learning it as easy as the web portrays it to be.

Rehaan

March 26th, 2011

Hey

Great article i really want to learn RoR Thanks for sharing this info.

sam fleming

March 26th, 2011

Fantastic article.

I too am a php developer who had a little look at RoR a while ago.

You’ve inspired me to take another look. Thank you!

Thomas McGee

March 26th, 2011

Very nice and timely article. I’ve been exploring the option of jumping into developing with Ruby on Rails mainly because of the fact that it makes development so efficient. The database queries alone are a huge timesaver and a reason rails is growing in popularity. Thanks for the article and the example! It’s definitely a help.

J Burke

March 26th, 2011

I just started going through the Rails for Zombies exercises yesterday, and I’m really surprised with how… intuitive RoR is. I’ve messed around with ASP.net (VB and C#) and PHP, and they definitely have their strengths, but I’ve found RoR to be at least on par, if not easier (as a front-end guy) to dive in to.

komiska

March 26th, 2011

cant wait to see your app live in the web! really a great idea!

Marc Gayle

March 26th, 2011

@Jonathan Thanks. Sign up and keep up to date :)

@Dayson, the truth is that if you are used to PHP (i.e. a non-PHP framework like Zend or Cake PHP) i.e. the procedural kind then it will be a difficult jump because you now have to learn how to do OOP as opposed to procedural.

I am still kinda struggling with that too, because I came from a C, C++ background where everything was functions and all that good stuff. But now, working with objects and methods and the separation does get a bit challenging. But the more I push through, is the more I am glad I did. It just feels like I am developing smarter.

Sure, the web does portray it to be easier than it really is – that’s why I wrote this post.

Anomalous

March 26th, 2011

Just to throw some perspective out there. I am a hardcore Rails developer; I find PHP syntax to be incredibly ugly and strange. It’s all about what you’re used to. The convention is great. My company has PHP apps from 2006 that we support and we have a lot of trouble maintaining them. At that time we made the switch to Rails, and there are also older 2006 and 2007 Rails 1 and 2 apps that need support which are much easier to dive into and maintain. Convention also makes it easy for me to upgrade a Rails 1 app to Rails 3 or retrofit a new ruby gem to Rails 1.

kisin

March 27th, 2011

the framework you choose to build your webapp is not relevant to the quality of the app.
django, rails, codeigniter, cakephp, …. choose the framework that you fell the most comfortable with.

Fazreen

March 27th, 2011

I know nothing about RoR but I have intention to learn them someday. To create and develop new web app is my next objective after I have learned how to create a website. By the way thanks for the article.

Avangelist

March 28th, 2011

I haven’t had a chance to really dig into Ruby past the basic download, setup and creating a new app repository.

I am certain that it has major benefits for those with no formal programming background.

Something worth pointing out was that you mentioned that the gem for sending email was one of it’s perks as it saved you time.

But the same as with any framework the real mastery comes from being able to write those kind of gems yourself same as with your example of WordPress.

Marc Gayle

March 28th, 2011

@Avangelist absolutely. I have not reached that stage yet, because I am just learning – but even though I am just learning Rails allows me to do some relatively powerful stuff relatively easily.

@kisin you are right. I am sure there are other frameworks that allow you to be just as elegant as Rails – but this article was just based on my limited experiences. It so happens to be with Rails, and I love it so I had to share :)

Kevin Thomson

March 29th, 2011

I’ve been thinking of learning a bit of RoR for a while now. I use PHP mostly but wanted to learn something new. How does RoR compare to Python which many people also say is easy to learn?

Jacob Gube

March 30th, 2011

@Kevin Thomson: I come from PHP, so I can give you a perspective of how it’d be like from PHP->Ruby. It is not bad if you’re open to new ways of doing things and having to tuck away some of the patterns you learned from PHP. Ruby is like super-OO where everything is an object.

This is what I suggest: Go through http://tryruby.org/ to see how the Ruby syntax works. To me, the best way to learn is to learn Ruby first, then Rails – that way you’re not trying to learn two things at once (it’s like learning how to theme WordPress/Drupal and not knowing PHP or trying to learn CodeIgniter or CakePHP without knowing PHP, it’s like putting the carriage in front of the horse).

I don’t know Python well; I messed around with Django, but I still liked PHP afterwards, so I decided not to go forward with it. Ruby is awesome enough to make me want to look outside of PHP. Except for a few things, like, I’m a terse coder, I don’t like that the coding standard that many follow is verbose over terser code like using do/end over curly braces.

And, it goes without saying, what I’ve said above is my personal opinion and personal preference. Your situation may vary.

Danny

March 30th, 2011

Hey,

Nice, easy reading article.
I am coming from PHP and been on the fence about RoR for some time. Maybe will give it a shot after this, hoping the installation on a Windows station is as easy as XAMPP…

Thanks.

Merengue

March 30th, 2011

Marc, congratulations! You had me a great improvement in just 8 months. Where did you start learning ror? What tools did you use(books, sites, groups, blogs, etc) to move forward with your project? Thanks.

Marc Gayle

April 5th, 2011

@Merengue Well I started doing everything everybody says – railstutorial.org, railsforzombies, tryruby.org like Jacob recommends.

Rails & Ruby are two different things. Ruby is the language and Rails is the framework. If you plan on going the Ruby way first, make sure you understand that distinction. That tripped me up for a while – because I was wondering when I would learn – in Ruby – how to do things that Rails does (not Ruby).

But honestly, where I really learnt the most (and I am still learning and have a far way to go) is general brute force. I started my web app and asked and still do ask LOTS of questions along the way.

Both on StackOverflow.com and on IRC in #RubyOnRails on irc.freenode.net and anybody else that I know is a Rails genius. I constantly pick their brain on how to attack problems.

I would say pick a smallish project, CompVersions turned out to be a pretty big undertaking for me – but it forced me to learn more, more quickly so I guess that’s good – so if you can find something small then I would encourage you to do so, and just try to build it.

You would be surprised how far you can go from doing it the hard way.

Web App

April 5th, 2011

Major paradigm shift from building apps with PHP. There are some modules for PHP and some very popular frameworks, however nothing like what you explained.

I’m sure there is a learning curve. Once that has been accomplished it seems one could breeze right through building an app.

Jacob Gube

April 6th, 2011

@Web App: Exactly. Major paradigm shift. It’s almost as if sometimes you have to trick yourself into thinking you don’t know PHP.

Marc Gayle

April 11th, 2011

@Jacob and @Web App I agree. It does require a shift in mindset – but I think that’s mainly because unless you used an OOP framework in PHP (like CakePHP) the vast majority of development in PHP is procedural (no?).

It definitely does take some adjusting, but once you go through it you will be pleased :)

Phil

April 12th, 2011

we recently start using RoR where I work and our development time for web applications has been cut in half…
We’ve been saving a lot of money this way and clients are obviously happy with the swift turnarounds!!

Jatin

April 22nd, 2011

Thanks Marc. I was thinking of developing an app using Rails, but your article gave me a boost to go ahead.

Sola

May 2nd, 2011

Good guy, good writeup, good idea. You would one day write a book on RoR. Pls write it in the same format you wrote this article. By the way I was trying to get in on RoR but couldn’t get anything like this writeup then. Now Drupal is my pal.

Tyler

May 15th, 2011

Great article. I’ve been working with rails for about 4 months and have the same issues with nothing but “Build a blog” tutorials.

Leave a Comment

Subscribe to the comments on this article.