Ruby on Rails + Netbeans: Creating a webapp in 10 mins

Posted on Friday, January 09, 2009 by Anuj Mehta

We will create a Ruby on Rails based simple web application which takes name of a person and greets him. Rails is ruby based framework for creating web applications. The framework is based on MVC (Model, View and Control) architecture. Model is the module that provides easy access of database; Controller has logic and data flow intelligence while the View displays data on browser. For this application we don’t need to store any data hence we will disable support for database access. For this example I am using Rails 2.1.2 and Netbeans 6.5. The application will finally look like this






1. In Netbeans click on File --> New Project. In the New Project dialogue select Ruby as category and “Ruby on Rails Application” as projects. Then press Next



2. In the next dialogue set project name as simple_web_application. Since we are not using database for this application press Finish.




3. Now Netbeans generates the project for you. Among the generated folders noteworthy are Controllers, Models and Views. You will see database.yml being opened in the editor. This file contains database related configuration parameters. Since in our application we are not using any database hence we don’t need to make any changes in this file. You can simple close this file.

4. Now we will generate the controllers and view for this application. Right click on the project node simple_web_application and select Generate option.




5. Now you see the Rails generator dialogue. In the generator drop down list select controller and specify names for controller and views. Here I had specified ‘main’ as controller and ‘page1’ as the view name. Press OK.




6. The Rails generator quickly generates the controller and views classes for you. The main_controller.rb is the controller class and page1.html.erb is view class. In view class we can put data in HMTL or RTHML format. The RHTML is the ruby HTML format. Following are things of importance for RHTML
• The ruby statements (code to be executed, but not included in HTML page) are enclosed in <% %>
• Ruby values to be included in the resultant HTML are enclosed in <%= %>


In the views we will create a form and add a text filed and submit button.
Add the following code in view class page1.html.erb


<% form_tag(:action => 'display_name', :controller => 'main') do-%>
<%= label_tag 'name','Name' %>
<%= text_field_tag :first_name, params[:first_name] %>

<%= submit_tag "Submit"%>


<%end-%>


Now we need to define the methods in controller. Put following code in main_controller.rb


class MainController < ApplicationController
def page1
end

def display_name
@name = params[:first_name]
end
end


Here we had defined an additional method “display_name” which will retrieve the name from text field and send it to second page which will greet the user.

7. We now need create one more view which greets the user. Right click on the sub tree node main in Views folder and select ERB file



8. Give the file name as display_name



Now add following code in display_name.erb file


Hello <%= @name %>



We also need to set the routes. Copy following code in /config/routes.rb file


ActionController::Routing::Routes.draw do |map|
# The priority is based upon order of creation: first created -> highest priority.

# Sample of regular route:
# map.connect 'products/:id', :controller => 'catalog', :action => 'view'
# Keep in mind you can assign values other than :controller and :action

# Sample of named route:
# map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
# This route can be invoked with purchase_url(:id => product.id)

# Sample resource route (maps HTTP verbs to controller actions automatically):
# map.resources :products

# Sample resource route with options:
# map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get }

# Sample resource route with sub-resources:
# map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller

# Sample resource route with more complex sub-resources
# map.resources :products do |products|
# products.resources :comments
# products.resources :sales, :collection => { :recent => :get }
# end

# Sample resource route within a namespace:
# map.namespace :admin do |admin|
# # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
# admin.resources :products
# end

# You can have the root of your site routed with map.root -- just remember to delete public/index.html.
map.root :controller => "main", :action => 'page1'

map.conect '/main/display_name',:controller => 'main', :action => 'display_name'
# See how all your routes lay out with "rake routes"

# Install the default routes as the lowest priority.
# Note: These default routes make all actions in every controller accessible via GET requests. You should
# consider removing the them or commenting them out if you're using named routes and resources.
#map.connect ':controller/:action/:id'
#map.connect ':controller/:action/:id.:format'
end


9. Delete the index.html file from /public folder. And the application is ready. Just Run it.

0 Responses to "Ruby on Rails + Netbeans: Creating a webapp in 10 mins":