Presented By
Code71 Bangladesh Ltd.
Organized by
BASIS
Preview
• Background
• Business Implication
• Introduction
• ROR and MVC
• Convention over configuration in rails
• ActiveRecord - The Rails ORM framework
• ActionView - The ROR view renderer
• ActionController - The core of a web request in ROR
• Data Migration
• Ajax
• Built in test framework
• Logging framework
• REST support
• Gems and plugins - Package manager for ruby
• Demo application - Build a web application in minutes
• Demo of a real life web application built on rails
• Future of rails
• Q&A
Extracted from real world application: As Hansson and others worked on the
application, they found out—for the hundredth time—that Web development is
painful, time-consuming, repetitive and detail-oriented. This made it a good
candidate for coding in Ruby. Goal was to build an application - Basecamp.
In the process of writing this high-level, condensed Ruby code, Hansson started
to abstract away the essentials of the interface, and the result became Ruby on
Rails. It was born from real-world needs, from working code and from the
everyday experiences of developers.
belongs_to :portfolio
has_one :project_manager
has_many :milestones
has_many :deliverables, :through => :milestones
end
• Rails redefined RAD (Rapid Application Development) for the web. Leveraging this
speed of development has shown to save both time and money.
• Rails also simplifies AJAX to allow for enhanced usability and a more satisfying end user
experience.
• Due to Rails mantra of "convention over configuration", those familiar with the Rails
framework can easily anticipate how the application works resulting in a highly
maintainable code base.
• When Rails first emerged, Rails had a reputation for not being scalable, but newer
versions of Rails have overcome that reputation and now power some of the largest
sites on the internet.
• Rails has a wealth of plugins and third party (commercial and open source) solutions
which prevent developers having to spend time and money reinventing the wheel for each
application.
Ajax it in no time.
What sets this framework apart from all of the others is the
preference for convention over configuration making
applications easier to develop and understand.”
Default route
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
View(app/views/hello/say_hello.rhtml):
<b>Hello <%=@user_name%>!</b> Welcome to demo app.
Browser Request:
http://localhost:3000/hello/say_hello
Browser Response:
What ?
Migration allows us to use Ruby to define changes to our database
schema incrementally, making it possible to use a version control
system to keep things synchronized with the actual code
Why ?
• Allows incremental development of database
• Allows easy roll out of a new version of a database
• Allows easy roll back to previous versions
• Allows to change the database anytime
def self.down
drop_table :companies
end
end
O
bject-relational mapping (ORM) layer supplied with Rails
C
onnects business objects and database tables
C
reates a persistent domain model where logic and data
are presented in one place
P
lays the role of model in MVC architecture.
Example:
Class Table Class Table
Name Name Name Name
Order orders Person people
TaxAgency tax_agencie Datum data
Batch s
batches Quantity quantities
Diagnosis diagnoses LineItem line_items
By Name:
u = User.find_by_first_name(“John”)
1. Company
2. User
3. Address
4. Role
V
alidations are methods to validate the contents of a
model object
I
f validation fails an object will not be written to the db. It
will be left in memory with its current invalid state.
V
alidations can be performed on save, create or update
Using a handler
class Order < ActiveRecord::Base
before_validation :normalize_credit_card_number
after_create do |order|
logger.info "Order #{order.id} created“
end
protected
def normalize_credit_card_number
self.cc_number.gsub!(/-\w/, '' )
end
end
• Can return ‘other’ data to client (other then html) like a pdf, or
word document.
• You can have multiple templates with the same name – but
different extensions. – ie. .rhtml, .rxml, .rjs.
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
render :action => :show
else
render :template => "fix_user_errors“
end
end
• 3 formats
– rhtml – html with imbedded ruby
– rxml – a programmatic way of constructing xml content
– Rjs – generates javascript.
def page_title
end
e
nd
--
--
<
h3><%= page_title %></h3>
Options:
1 :multipart - If set to true, the enctype is set to "multipart/form-data".
2 :method - Usually either "get" or "post". If "put", "delete" or another verb is used, a
hidden input with name _method is added to simulate the verb over post.
3 A list of parameters to feed to the URL the form will be posted to.
Example:
<% form_tag {:controller=>”product”, :action=>”create” } do %>
<%= text_field_tag(:product_name) %>
<%= submit_tag(“Save") %>
<% end %>
Generated Code:
<form action="/product/create" method="post">
<input id="my_name" name=“product_name" type="text" />
<input name="commit" type="submit" value=“Save" />
</form>
Creates a form and a scope around a specific model object that is used as a base for
questioning about values for the fields.
Example:
<% form_for @product do |f| %>
<%= f.text_field :name %>
<%= f.text_field :price, :size => 10 %>
<%= f.text_field :amount, :size =>5 %>
<%= submit_tag "New Product" %>
<% end %>
Generated Code:
<form action="/products“ id="new_product" method="post">
<input id="product_name" name=“product[name]“ type="text" />
<input id="product_price" name=“product[price]" size="10" type="text" />
<input id="product_amount" name=“product[amount]" size="5" type="text" />
<input id="product_submit" name="commit" type="submit" value="Update" />
</form>
Copyright 2010, Code71 Inc. All rights reserved.
Handling errors in form
error_messages and error_messages_for
Example:
Display all of the error messages
<div id="current_time">
</div>
<div id="current_time">
</div>
<p>Text to reverse:
<%= text_field_tag 'text_to_reverse' %></p>
<p id="reversed"></p>
<p><%= submit_tag 'Reverse!' %></p>
def alert_with_rjs
render :update do |page|
page.alert "Hello from inline RJS"
end
end
<%= link_to_remote "Alert without RJS", :url => { :action => "alert_without_rjs" } %>
<%= link_to_remote "Alert with RJS", :url => { :action => "alert_with_rjs" } %>
Controller.rb:
def alert_with_rjs
end
Views/controller/alert_with_rjs.rjs
page[:my_div].show
Generates javascript
$('my_div').show();
replace_html.rjs
page[:my_div].replace_html "New Text“
replace.rjs:
page[:my_div].replace "New Text"
replace_html replace
Original <body> <body>
<div id="my_div"> DIV </div> <div id="my_div"> DIV </div>
</body> </body>
RJS page[:my_div].replace_html page[:my_div].replace
"New Text" "New Text"
Result <body> <body>
<div id="my_div"> New Text
New Text </body>
</div>
</body>
By default, Rails will log all levels (debug and higher) in every
environment except production.
In the production environment, it will only log info and higher. This
behavior can be changed in the configuration for each
environment.
• Stored in test/fixtures/
Functional Test
• Written against controller instance, simulate request
against it, and make assertions about the responses
• Testing library placed on test/functional/ and file name
like controller_name_test.rb
def test_get_user_by_company
users =
User.get_user_by_company(companies(:one).id)
assert_not_nil users
assert_equal 3,users.length
end
end
def setup
@controller = UserController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@request.env['HTTP_HOST'] = "localhost"
end
def test_get_user
get :show, :id => users(:admin).id
assert_response :success
assert_not_nil assigns(:user)
end
end
assert_template ‘store/index’
Examples:
• 20.minutes.ago
• Time_ago_in_words
• 1.gigabyte
• Titelize
• Humanize
R
ails core: Yes, Rails is a full-stack framework and will remain so,
but there’s no reason we shouldn’t also make it possible to run with
less than the full monty. Rails 3 will make it easy to run just a bare
minimum and then allow you to opt in just the stuff you want, if
that’s necessary for your particular situation. Think “rails myapp—
core” (and “rails myapp—flat”).
F
ramework agnosticism: Rails will always have a default answer to
every question within the stack. If you don’t care about testing
frameworks, you’ll get test/unit. If you don’t care about which ORM,
you’ll get Active Record. But some people do care and want
something else. Some people want RSpec for testing, others want
to use Sequel or Data Mapper for ORM, others again prefer Haml
for templating, and some might prefer jQuery for Ajax. All these
people should feel like Rails is welcoming them with open arms.
Yes, we’ll have a default, but we shouldn’t have any form of
discrimination against alternatives.
Copyright 2010, Code71 Inc. All rights reserved.
Future of Rails-Rails 3
ScrumPad
www.scrumpad.com
F
or further queries or interest in RoR training contact:
t
raining@code71.com
w
ww.code71.com/training.aspx
Q&A
http://wannabesoftwareengineer.blogspot.com/
http://www.fuadcse.blogspot.com
Thank You