inicio mail me! sindicaci;ón


db.networx

working with Rails 2.0, Ruby on Rails, Flex, Flash and …


Tutorial >> Running on Rails with Ruby on Rails using Netbeans 6 and MySQL 5

July 6, 2007 at 22:26

Recently I wrote a Tutorial on how to build a Running JSF Application with Facelets, Glassfish, JPA and a MySQL 5 server. Interesting for me now is, how long will it take to do the same with Ruby on Rails and Netbeans 6.
So what is to be done to get started working with Netbeans 6, MySQL and Ruby on Rails? Here is a short list what you need, please refer to the installation guide inside Netbeans 6 on how to use Ruby on Rails and how to install Ruby:

Ruby on Rails Applications are always combined with a Database. MySQL5 is one of my favorite databases. So before we start coding, we need a database where the data of our application goes. The easiest way is to install the MySQL Tools, like the MySQL Administrator and Workbench. Having done so you can very quickly create a database an name it “runningonrails_development”. Create a MySQL User called “running” and a password called “forrunning”.

Now we can move over to Netbeans 6. Start the IDE and select File -> New Project. Select the folder “Ruby” and mark “Ruby on Rails Application” on the right side. Click next and name the application “runningonrails”. Hit the finish button.

Name your Running on Rails Application

Now you should see the initial folder structure of your freshly generated Rails Application:

The initial folder structure of Running on Rails

In this tutorial I won’t go into detail on how Ruby on Rails is structured and what the MVC concept is all about, because there are a lot of good tutorials describing that in detail. We’ll just build a small application here.

Before generating our tables and model we need to tell Ruby on Rails where our MySQL Database is hiding and how Ruby on Rails can get in contact to it and our “runningonrails_development” database. So navigate to the folder configuration and open “database.yml”. Here we’ll just edit the following entry:

Configuring the Database Connection in the database.yml

Now we’ll create our model class. Netbeans 6 has some tools ready for you to help you and make the generation process of classes and other files easy to manage. Right-Click on the project and select Generate. You will be confronted with the following dialogue:

Rails Generator

Select model in the drop-down box and insert the following arguments: run date_of_run:date location:string total_hours:int total_minutes:int totalSeconds:int avg_hr:int distance:float — this will generate a model class and also create a database migration script with the columns listed directly behind the model name “run”. Run the generator, afterwards you’ll find a new migration script in Database -> migrate and a new model class. we’re almost done. Let’s run the migration script to produce the columns we just created with the model. Right-Click on the project name and select run rake target -> db -> migrate – now you can watch the table and columns being created. We are one step away from filling our application with data.

Again right-click on the project head and select “Generate”. Navigate to scaffold in the drop-down menu and select it. scaffold is a nice, which creates the CRUD view for us. CRUD stands for create read update and delete. We only need to tell the generator for which model we need the scaffold and the rest will be done by scripts inside Ruby on Rails. Type run into the Model-Name box an hit the ok button.

It’s time :-) we are ready to start our small application, to do so, right-click on the project name and select “run project”. This will start the development server for you. The server shows you a welcome page. To call our model, we just have to type in localhost:3000/runs and we will see our column names listed, now we can start to fill in the data!

Whats with a facelets-like technology in Rails? Well in Rails its called templating, you can define many different layouts and use them across your application. So let’s create a standard layout for our small running application: Take a look at the file runs.rhtml under views -> layouts. It’s a template and used whenever a method of the runs class is called. The of every subpage is written in the place where the following declaration is placed: <%= yield %>
If you want to extend your running application and have more than one class, you can define a layout that applies to every class. To tell the other classes to use that specific layout just integrate the following line at the top of each controller: layout “yourspecificlayout” – yourspecificlayout needs to be in views -> layout and needs to end with .rhtml — We will be using the given runs.rhtml extending it by the following lines:

<div style=”width:800px;height:70px;padding:10px;border:1px solid #CCCCCC;font-size:16px;”>
Running on Rails –> <a href=”list”>show all runs</a> — <a href=”new”>new run</a>
</div>
<div style=”width:800px;padding:10px;padding-top: 20px;border: 1px solid #CCCCCC;”>
<%= yield %>
</div>

Well that was a bit quicker like in my tutorial about Java Server Faces.

ruby on rails sample application running

5 Comments »

  Paul wrote @ September 12th, 2007 at 23:09

Thanks for the tutorial. It was really helpful for me, a newby. Please write more RoR tutorials if you have time:-)

  NguyenVu wrote @ September 29th, 2007 at 06:37

I second that, thank you for your RoR tutorials :)

  :: aina :: wrote @ February 13th, 2008 at 08:32

thanks for this simple tutorial.

i’m newbie i RoR development.
i’ve got an error when i tried to script/generate scaffold Publisher ‘admin/publisher’

but when i just put model name it’s execute well. i want to include publisher folder under admin which i’ve already generate earlier.

  dboek wrote @ March 22nd, 2008 at 13:10

Hey aina,
you could do the following: Generate publisher as a model class like you already did. Then you create the controller with –> script/generate controller ‘admin/publisher’ — now you have the publisher controller directly in the admin folder where you wanted it. Unfortunately you have to write the functions of the controller by yourself, but it’s easy and it’s a good practice for working with Ruby on Rails. Greets, Daniel

  Amit paul wrote @ May 7th, 2011 at 09:28

Hi
I want to use Ruby on rails in my (jsp, servlet, postgreesql8.4, jboss server) project. But i am new in Ruby on rails.

So give me some suggesition or some book name .

Your comment

HTML-Tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>