Create An eBay Clone With Ruby on Rails?
I found an interesting topic recently in a forum discussion. It was about whether or not an eBay clone could be build on the top of Ruby on Rails. A few interesting questions are: “Is RoR the best toolkit for doing that?”, “Whether the same eBay functionality could be accomplished?” or “Could it scale enough?”
Well, I come from the Java world and I think that Java is the best tool for such a “big enterprise application” as eBay. However Java will be appropriate if you own a big company and have lots of programmers that are ready to support such an application. It’s really really really harder to support a java application than Ruby on Rails equivalent.
So, when it comes to smaller teams, flexibility and agile development – Rails should be your last stop for a toolkit. What about the functionality? – Almost every programming language could cope with the problem of accomplishing the same functionality.
But again, if our target is a web 2.0 application with a database back end – the most agile and easy way would be to use Rails.
The effort will be the smallest and the maintainability will be the greatest. May be, the most serious problem is – “Could it scale enough?”. Well, an eBay clone is a serious application to serve. I bet that you are imagining your application having hundreds of thousands of requests in a second! If you create and populate such an application, lets say a local clone for your country, the traffic will grow gradually.
That means you will have the chance to see where the bottle-neck is – before having your app smashed of requests. And of course that traffic in most cases will be equivalent to money, so you will be really motivated to optimize and you will. So on the question “Could it scale enough?” , I’ll answer – YES. In case that you earn money and have the real problem in front of you – you will make it happen your application to scale.
So, in case that you are an Internet entrepreneur – think about the opportunity of creating an eBay clone for your country! Whether you’re selling cars, bikes or BBQ Grills, Take the Ruby on Rails toolkit and go ahead for “Creating an eBay Clone” – there’s nothing impossible!
Developing a News site in Rails
Recently I was asked the following question:
“How hard is it to build this project in Rails”
You can substitute this with anything. After a short period of time I realized that such a question can’t be really answered to a point where a programmer would agree completely if he/she didn’t even touched Ruby on Rails. In order to change that fact a person should see the whole process of developing and deploying a Rails application so he/she can realize the power and potential of a Rails application.
With all this in mind I hereby announce the Project type articles. Those articles will represent an overview of the development process of a real working Rails web application. This article is the first of the series Project and it will begin not so briefly and give a more detailed overview of the things we will do. The next articles in the series will focus on more complex things and will skip those explained before assuming the knowledge was already obtained.
Let’s begin with our first web application project:
Project 1 – Developing a News site in Rails
parts count: 3 (this is a three day project)
Rails version used: 1.2.3 (used both on Windows and Linux platform)
Ruby version used: 1.8.5 (used both on Windows and Linux platform)
Knowledge of HTML and CSS: assumed
Knowledge of a Web Programming Language and Database: Basic-Advanced assumed
While for our first application we might not need any real programming experience with Ruby or Rails, it is always a plus. Assuming you are not coming as a completely newbie not knowing anything about computers, WEB or HTML I will continue. If you feel otherwise please come back when you gain some experience at least enough to understand what we are going to do next.
The first and the second steps of our project are to generate and then prepare/configure our Rails application environment. For doing this we need to open a console (this is a bit tricky on windows depending on what you use; consult our forum if you have trouble). In it we need to navigate to our desired and beloved work directory and type:
Of course project1 is just a name and we can type in anything we like. After the command is issued we have a basic Rails application setup for us to use. We then need to enter the created directory and open the file located in config/database.yml and then edit it with your IDE or Editor of choice. The file contains the configuration of our database used. There are three databases:
Those three databases are used as their corresponding names. We should change the options to match the database we will use. The options are straight forward and do not require explanation. For now the only thing we need to change here is the development database. It needs to match the one we will use. After this is done, save the file and go back to the console. Now we start the server with:
This command will probably start the WebBrick server but depending on the environment and platform it might also start a Mongrel web server. There isn’t much to worry about this at this stage so assuming everything is fine we continue and open our web browser. To view our web application we need to go to:
Remember the ruby/rails applications run on port 3000 by default. So going to that address will send a request to our web server and it will kindly return us the default Rails application screen. Till now we have the first step done.
We have a Rails application running! Congratulations!
It wasn’t hart was it ?
Now as we have already setup our Rails application we need to start describing how it will work. As you know a Rails application has ONLY ONE WAY OF DEVELOPMENT. That one way is called MVC – Model-View-Controller. I will not go into detail about MVC but in short words it solves the problem with separating core logic from data representation. And speaking of it we need to define our first application controller. In the console again we type (as we used the first console for starting the server we might need another one):
This automatically generates our first controller named home. It is to be found in app/controllers/home_controller.rb. For now this is the only thing we do on the controllers part. In order for a MVC application to work we need to create models. Models describe our application behavior. But as we for example do our first application in Rails we will put aside our models for a while. First let us navigate our browser to the controller we just create.
OK this returns an unknown action error. OK this is good. We didn’t specified anything in our automatically generated controller. Now open the controller with your favorite tool of choice. As you see Rails generated some basic controller for us. Now we need to code a bit:
What we just did to the controller ?
We defined a default action. As everything on the web, INDEX is always the default action. We redirect the index action to an other action named list_stories. And as a second we defined that list_stories action we redirect to. Now Save the file. Refresh the browser!
Now the error message has changed. It now says that it has no view (also named as a template) to render.
OK then we shall create it in app/views/layout/home.rhtml.
You see how logical the structure is ? First we create a layout design for the controller then for the according action. Don’t bother question the code in the home.rhtml I will soon explain it.
Then create one in app/views/home/list_stories.rhtml.
Now as we are building a complete web site we need to have some standard layout for all the pages. Instead of doing nasty includes all the way in our pages like the one created, Rails offers us something different. Put these in the layouts folder:
layout.css (create it in public/stylesheets/)
Now let’s look back to the layouts folder where we have this home layout. It reflects the name of our controller so we can easily by intuition find it. What it does? It render partials. Partials are those others files we created in the same directory starting with _name. That dash specify that this is a partial so we can just call it by the name. We can render partials from other directories if we specify a /directory/partial_name but for now we don’t need this. And what is this <%= yield :layout %> for ?
<%= yield :layout %> is used for rendering the current action of that controller. So if we have two actions and they use their default views they would be rendered at that point. Breeze! Now refresh your browser! We should now have a complete (but still basic) layout of our web site. Still we did a pretty static thing but don’t worry 1/3 of the job is done
We can now rest peacefully cause we did our job for today.
Tomorrow we will do our core logic.