This week I was appointed with the task of doing a web interface in order to distribute the tasks between the nodes of a cluster. And to do this, Ruby on Rails was recommended. So here, I'll explain a short example that I did based on a tutorial. But first, the installation.
Installing Ruby On Rails
Now I'm not planning to go on a lot of details with the installation, because it took me a while to figure out how to fix some errors that I kept getting at the installation. But I'll explain the steps in short. These steps can be found here: http://rubyonrails.org/download anyway if you don't want to read them here again(but with a little more explanation).
1. Install Ruby if you don't have it already
You can download the source and compile it manually. The source can be downloaded from here(Ruby 1.8.3).
Decompress it, move to the decompressed folder, and to compile it run the following command:
$ ./configure && make && sudo make install
Wait until it finishes.
2. Install RubyGems
As the Ruby on Rails describes, "RubyGems is the standard Ruby package manager.". To install it, we can download the source from here.
Again, decompress it, move to the folder and to install it run the command:
$ ruby setup.rb
(You'll probably need to be with sudo/root privileges)
3. Install Rails
To install Rails, you can just use gem to install rails and all its dependencies using the command:
$ gem install rails
Note: This command will show no sign of activity at the beginning(which I misinterpreted as the command not working, so I kept cancelling it). To check what its actually happening, add a -V flag to the command to force the install to verbose output.
$ gem install rails -V
4. Make a new App
If everything was installed correctly, it is the time to make a new application. To do this we can just use the following command:
$ rails new appname
This will create all the necessary files and directories that you'll need to create your application. Now we can build our gems (packages) that we're going to use in the application. We can check them in the Gemfile in our appname/ directory. To build the gems, just use the command:
$ bundle installDemo App
Now, to test the web interface, first we'll need to start the server. To do this, just move into the application folder and run the following command:
$ rails server
Now we can check on our favorite browser, the address http://localhost:3000/.
Demo App
I found this tutorial pretty good for starting with ruby on rails. It shows the way to create resources using the scaffold command and giving some parameters to the command as well. I imagine this resources/models as objects, because you can give them attributes of what you want them to have.
The example there on the tutorial is an user resource. So if we want to in fact, create an user, that user should contain the following attributes:
The scaffold command should contain the following:
$ rails generate scaffold User name:string email:string
The id is automatically added, so it is not necessary to define it.
This would generate an users data model that will be stored in the database. To access it, we'll need to update the database using the command migrate, like the following:
$ bundle exec rake db:migrate
Doing this, will add some functionality to the web page. We'll be able to see all the registered users, add new users, and edit already existing users. Each of these functions are in the directions:
URL | Action | Purpose |
---|---|---|
/users | index | page to list all users |
/users/1 | show | page to show user with id 1 |
/users/new | new | page to make a new user |
/users/1/edit | edit | page to edit user with id 1 |
Here are some screen captures of the web app.
Now this is cool, but this will not provide any kind of validation in the input, so how would we add some validation or other personal codification to the web app. To do this, we can modify the user.rb file located in the appname/app/models directory. Here we can write code (In Ruby, or Rails), to do what we please with the data models. For example, lets do a validation to the input at the user's email making it shorter than 30 characters, and longer than 10. To do that we can make the following changes to the appname/app/models/user.rb file.
class User < ActiveRecord::Base validates :email, :length => { :maximum => 30, :minimum => 10} end
Now, if we try to input an email shorter than 10 characters, the web app should produce the following error:
And if we try to input an email larger than 30 characters, the following error should be produced:
And this is the end of an easy app. This doesn't mean that this is all of ruby on rails. Obviously this is just the basics, and ruby on rails has a lot more to offer to help in creating a better web app.
References:
I will use this to boost the dist lab to 8 on week #8.
ResponderEliminar