AShortRubyOnRailsTutorialForNetbeans6

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.

File:640

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

File:initial_folder_structure_AShortRubyOnRailsTutorialForNetbeans6.png

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:

File:configuring_database_yml_AShortRubyOnRailsTutorialForNetbeans6.png

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:

File:640

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:

Running on Rails –> <a href=”list”>show all runs</a> — <a href=”new”>new run</a>

<%= yield %>

Well that was a bit quicker as in my tutorial Using JSF and Facelets in Netbeans.
Feel free to leave a comment on my Blog by clicking here Ruby on Rails with Netbeans 6

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo