RubyOnRails Ebook
RubyOnRails Ebook
Ruby
It is a dynamic, general-purpose object-oriented programming language Combines syntax inspired by Perl, also influenced by Eiffel and Lisp Supports multiple programming paradigms, including functional, object oriented, imperative and reflective
Ruby(Contd.)
Has a dynamic type system and automatic memory management
Ruby is a metaprogramming language.
# Output Bhaskar in upprecase puts Bhaskar .upcase # Output Bhaskar 10 times 10.times do puts Bhaskar .upcase end
New Employee
Creating an instance of the Employee class: a = Employee.new(JAY", 23", Test Engineer") b = Employee.new(SAM", 24", Test Engineer")
Method
To be able to describe employees, we add a method to the employee class: def describe @name + " is of " + @age + " years" +" working as " + @position+ ".\n" end
Calling Method
To get the description of Employee, we can call Employee with the describe method attached : emp= a.describe puts emp or: puts a.describe
Rails
Rails is an open source Ruby framework for developing database-backed web applications The Rails framework was extracted from real-world web applications. Thus it is an easy to use and cohesive framework that's rich in functionality
Rails(Contd.)
All layers in Rails are built to work together and uses a single language from top to bottom
Everything in Rails (templates to control flow to business logic) is written in Ruby, except for configuration files - YAML
MVC pattern allows rapid change and evolution of the user interface and controller separate from the data model
Model
Contains the data of the application Transient Stored (eg Database)
Enforces "business" rules of the application Attributes Work flow
View
Provides the user interface
Dynamic content rendered through templates Three major types Ruby code in erb (embedded ruby) templates xml.builder templates rjs templates (for javascript, and thus ajax)
Controller
Perform the bulk of the heavy lifting
Handles web requests Maintains session state Performs caching Manages helper modules
I have created an application which can be accessed at http://ancient-ridge-9795.herokuapp.com/ It consists of a Home page which displays text And has two right and left buttons, which can be used for displaying and adding messages.
Steps Involved
Creating application on the local host
These are the files which rails automatically generates to create the framework for our application.
Step 3:
Create the needed controller, model and views for our application I will keep simple functionality in which a user can post message
$rails generate scaffold post name:string address:text
Scaffold command creates a CRUD (Create, Read, Update, Delete) interface for app ( a quick way of creating the MVC automatically). Alternatively, we can also create our controller, model and view manually using the command
// for creating controller rails generate controller <controller name> // for creating model rails generate model <model name>
Step 4:
Create Database rake db:create
Step 5:
Since we have recently created a new model for Post, a table must be created in our database and requires that we upgrade the database using this command: rake db:migrate
Step 5:
Creating a home page $rails generate controller home index
This creates a controller home along with views in the app/view/home directory. Rails will create several files for you, including app/views/home/index.html.erb file. This is the template that we will use to display the results of the index action (method) in the home controller. Open this file in your text editor and edit it to contain the code that you want to display in your index page
Editing homepage
Open file app/views/home/index.html.erb and edit it to contain the code that you want to display in your index page
The See Messages and Write Messages will appear on the main page
The See Messages and Write Messages will appear on the main page
The See Messages and Write Messages will appear on the main page
References:
ppt, Ruby on Rails, A new gem in web development ppt, Ruby Intro Ruby on Rails tutorial book
http://en.wikipedia.org/wiki/Ruby_on_Rails http://fuelyourcoding.com/creating-your-firstruby-on-rails-application-from-scratch/