WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this Article to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES SUBMIT AN ARTICLE PRINT
Title : Simple application in Ruby on Rails
Categories : Ruby on Rails, Beginner Guides
Boaz Yahav
Boaz Yahav
Date : 2008-03-06
Grade : 3 of 5 (graded 7 times)
Viewed : 9073
Search : More Articles by Boaz Yahav
Action : Grade This Article
Tools : My Favotite Articles


  Submit your own code examples 
 


Ruby
Ruby is an open source, object oriented scripting language introduced by Yukihiro Matsumoto (known widely as “Matz”) in 1994 and was officially released by Matz in 1995. It is a scripting language with features similar to the Python and Perl.

Rails
Rails framework was designed and given by David Heinemeier Hansson in 1994. It was developed under the MIT License. The web application framework given by David Heinemeier Hansson was named as “ Ruby on Rails”.

Ruby on Rails
Ruby and Rails are taken together because Ruby is the base foundation for Rails ( Rails is a full-stack web application framework written in Ruby) so they have a sort of parent child relationship with each other.
Ruby on Rails is an environment which provide us productive framework along with the concepts of Object Oriented programming and hence is a very efficient way of developing fully featured web applications in less time and with less coding.
For working with Ruby on Rails, we can use the InstantRails installer which installs Ruby, Rails, the Apache web server and MySQL in one step.

When we unzip InstantRails software in our machine, we find a file InstantRails.exe in the folder where we have unzipped the files of InstantRails (I have unzipped the files in D:\InstantRails folder). When we double click on this InstantRails.exe file, we get an icon in the form of capital I on the task bar. When we right click on this icon in the task bar, we get a popup window from where we select the option : Open Ruby Console window as shown :




We get a command prompt where we can start building our application. Since, we our going to make our web application by name : shopping, we give following command :


D:\InstantRails\rails_apps>rails shopping




This command generates many files in their respective folders as shown below :


create
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create components
create db
create doc
create lib
create lib/tasks
create log
create public/images
create public/javascripts
create public/stylesheets
create script/performance
create script/process
create test/fixtures
create test/functional
create test/integration
create test/mocks/development
:::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::


We can see that a list of the files under respective directories are created by Rails. All these files and sub directories are created in a folder named : shopping. The folders that are created are as shown :




The \app directory, for example, contains \controllers, \helpers, \models and \views. The \views directory itself contains a subdirectory, \layouts, and so on. We go into our rail application directory :


D:\InstantRails\rails_apps>cd shopping


Now, to run the server, can give either of following command :


D:\InstantRails\rails_apps\shopping>ruby script/server
D:\InstantRails\rails_apps\shopping>ruby script/server webrick


Here script is a directory which was created when we executed the rails command and server is the name of a code file which runs the WEBrick server.




Now fire up a web browser. Enter the host name, followed by a colon and the port number into its address bar. The host name is localhost and the port number by default is 3000. Here is an example:

http://localhost:3000/
The browser should now display a page welcoming us aboard Rails as shown :




Creating a Controller
The Controller is the part of the application between the View (what appears in the browser) and the Model (Data). Let’s create a Controller named : Welcome.

We open another ruby console window (by right clicking on this icon in the task bar, and selecting the option : Open Ruby Console window ) and go to our shopping folder and run the following command :


D:\InstantRails\rails_apps\shopping>ruby script/generate controller Welcome






Several files and directories are created as shown above.
Rails has parsed the name Welcome to welcome into lowercase words. If our controller is of two words say WelcomeWorld, it will be parsed into welcome_world i.e. separated by an underscore. It is so because of the ‘configuration by convention’ approach used by Rails.

We will first make use of welcome_controller.rb file created in \shopping\app\controllers folder. Open this file in a text editor. This empty method has been auto-generated:


class WelcomeController < ApplicationController
end


Inside this class we can write some code to be executed when a certain page is displayed.
Suppose, we edit this file and make its contents appear as :


class WelcomeController < ApplicationController
def index
render :text => "Welcome to our Shopping Mall "
end

def exit
render :text => "Thanks for visiting"
end
end


This controller file now contains two methods: index and exit. Each method contains a single line of code. Render is a method which takes a Hash as an argument; the Hash itself containing a key-value pair comprising a symbol and a string. It can be rewritten like this:


def index
render( { :text => " Welcome to our Shopping Mall " } )
end


Here in above methods : index and exit, we are just rendering (outputiing) a certain text to the browser. That is, when the browser accesses the web site and passes the method name to it, the respective text is rendered on the screen.


Open the web browser and enter the full address of the two functions : index or exit. The address becomes : http://localhost:3000), plus the name of the controller (/welcome) and finally the name of a specific method (/index or /exit) :
http://localhost:3000/welcome/index

The output that we get is :




Similarly, if we give following address in the browser :
http://localhost:3000/ welcome /exit

The output that we get is :




In fact, Rails uses the index method as a default so we can even omit that part of the URL. That is, index method is automatically invoked even by following address :
http://localhost:3000/welcome









PHP 101 Part 5 of 15 : Rank And File
Categories : PHP, Beginner Guides, Filesystem
Who's Linking?
Categories : PHP, Beginner Guides, To PHP
Grabb'n and Pars'n
Categories : Beginner Guides, PHP, To PHP
PHP 101 Part 12 of 15 : Bugging Out
Categories : PHP, Beginner Guides, Errors and Logging
PHP 101 Part 2 of 15 : Calling All Operators
Categories : PHP, Beginner Guides, Operators
XSL Basics Part 1 of 2
Categories : XSL, Beginner Guides
Using If Else Statements
Categories : PHP, Beginner Guides
PHP and OOP
Categories : PHP, Object Oriented, Beginner Guides
Start Using MySQL
Categories : MySQL, Databases, To MySQL, Beginner Guides
Jump Start to Easy URLs
Categories : PHP, Beginner Guides, MySQL, File System, To PHP
PHP 101 Part 13 of 15 : The Trashman Cometh
Categories : PHP, Beginner Guides, Data Validation
Smarty Introduction
Categories : PHP, Smarty, Beginner Guides
Beginners guide to PHP and MySQL - Creating a simple guest book
Categories : Beginner Guides, To PHP, To MySQL, PHP, MySQL
How To add paging (Pagination) with PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, HTML and PHP
Counting - Creating a simple counter
Categories : PHP, MySQL, Beginner Guides, To PHP, To MySQL