boston computing review - ruby on rails
DESCRIPTION
This presentation provides a high level overview of the Ruby on Rails development framework.TRANSCRIPT
![Page 1: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/1.jpg)
Boston Computing Review 2006
BCR : Ruby on Rails
an introductionmore at http://www.rubyonrails.org/
John Brunswick
![Page 2: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/2.jpg)
Boston Computing Review 2006
Background / Agenda
• Upcoming web project
• Fears
• Welcome Ruby
• Getting Onto Rails– Framework, MVC, Databases, Application
Servers and more
• Deploying ROR
• Where to look Next
![Page 3: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/3.jpg)
Boston Computing Review 2006
Considerations
• .NET– Experience
• J2EE– Enterprise Acceptance
• PHP– Proven, knowledgebase
• Ruby on Rails– Development Speed, native MVC *and a little
fun*
![Page 4: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/4.jpg)
Boston Computing Review 2006
Fears
• PHP vs ROR
• Stability
• Lack of Public Knowledge Base
• “Shark Attack”
• Missing Intelisense…
![Page 5: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/5.jpg)
Boston Computing Review 2006
Ruby
• Based on SmallTalk, Perl, Lisp
• Object Oriented
• Not strongly typed• Basics http://www.fincher.org/tips/Languages/Ruby/
• “FUN”? :)
![Page 6: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/6.jpg)
Boston Computing Review 2006
Rails Framework
• Power Through Rules and Best Practice• MVC• Assumes a Database• Object Relational Mapping• Forms Handling – Been there done that• Parameters• Link Building• Scaffolding
![Page 7: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/7.jpg)
Boston Computing Review 2006
MVC
• Nothing New, 1973
• Model– Your Data and Data Rules
• View– Interface
• Controller– Traffic Director
![Page 8: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/8.jpg)
Boston Computing Review 2006
MVC in ROR
View ShowUser.rhtml
<html>
…
User Name<% = @user.name %>
…
</html>
ControllerUsers_controller.rb
…
def ShowUser
@user = User.find(params[:id])
end
def otherend
def anotherend
…
Modeluser.rb
class User < ActiveRecord::Base
#relations has_many:posts
#start validation here validates_presence_of :email, :username, :password validates_uniqueness_of :email, :username
End
![Page 9: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/9.jpg)
Boston Computing Review 2006
Model
• Object Relational Mapping
• “ActiveRecord”
• Less Database “glue” Code *sigh of relief!*
• Worst Case Scenario Optimizations Possible with manual SQL
• Logging for Performance Checking
![Page 10: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/10.jpg)
Boston Computing Review 2006
Model : Rules
• Table Names– Plurals
• Attribute Names– id for primary key in table– table_id for foreign key in other table
• Ability to run joins via objects!– Article.User.Username
• Legacy Options Available
![Page 11: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/11.jpg)
Boston Computing Review 2006
Model : Sample from Text
![Page 12: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/12.jpg)
Boston Computing Review 2006
Model : Sample from Text
![Page 13: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/13.jpg)
Boston Computing Review 2006
Model : Sample from Text
![Page 14: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/14.jpg)
Boston Computing Review 2006
Model : Code Sampleclass User < ActiveRecord::Base
#relations has_many:posts #related to rankings has_many:ranks has_many:critiques #start validation here validates_format_of(:email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message=>"has an invalid format") validates_presence_of :email, :username, :password validates_uniqueness_of :email, :username #authentication for user def self.authenticate(username, password) user = User.find(:first, :conditions => ["username = ?", username]) if user if user.password != password user = nil end end user end end
![Page 15: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/15.jpg)
Boston Computing Review 2006
Model : DB LoggingProcessing SearchController#list (for 127.0.0.1 at 2006-11-18
22:51:36) [POST] Session ID: 667befe9190e1c686f537e8dcdcd731d Parameters: {"commit"=>"search", "action"=>"list",
"controller"=>"search", "query"=>{"query"=>"test"}} [4;36;1mArticle Load (0.000000)[0m [0;1mselect a.* from articles a
where (lower(a.title) like '%querytest%' or lower(a.articlebody) like '%querytest%' or lower(a.description) like '%querytest%') order by a.created_on desc[0m
Rendering within layouts/searchRendering search/listCompleted in 0.03100 (32 reqs/sec) | Rendering: 0.01500 (48%) | DB:
0.00000 (0%) | 200 OK [http://localhost/search/list]
![Page 16: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/16.jpg)
Boston Computing Review 2006
Controller
• Method name matches view folder– users_controller.rb works for
/views/users/***.rhtml– called “actions”– all view’s methods will sit there
• Ability to– CRUD– Flash– Redirect
![Page 17: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/17.jpg)
Boston Computing Review 2006
Controller : ActiveRecord Create
def create @user = User.new(params[:user]) if @user.save flash[:notice] = 'User was successfully
created.' redirect_to :action => 'list' else render :action => 'new' end end
![Page 18: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/18.jpg)
Boston Computing Review 2006
Controller : Flash
def create @user = User.new(params[:user]) if @user.save flash[:notice] = 'User was successfully
created.' redirect_to :action => 'list' else render :action => 'new' end end
![Page 19: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/19.jpg)
Boston Computing Review 2006
Controller : Redirect
def create @user = User.new(params[:user]) if @user.save flash[:notice] = 'User was successfully
created.' redirect_to :action => 'list' else render :action => 'new' end end
![Page 20: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/20.jpg)
Boston Computing Review 2006
Controller : Getting Data
• Request Data (POST / GET)– Params hash
• Models
• Session data
• etc
![Page 21: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/21.jpg)
Boston Computing Review 2006
Views
• Show the data
• Templates (layouts)
• Use objects from controller
• Navigate guide into controller / action
• Forms
![Page 22: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/22.jpg)
Boston Computing Review 2006
Views : Showing Data
• Inline Ruby (similar to JSP)
<% for column in User.content_columns %><p> <b><%= column.human_name %>:</b> <%=h
@user.send(column.name) %></p><% end %>
<%= link_to 'Edit', :action => 'edit', :id => @user %> |<%= link_to 'Back', :action => 'list' %>
![Page 23: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/23.jpg)
Boston Computing Review 2006
Views : Layouts
• Inherit by default for controller
• Exception in controller#set the layout
layout "articles", :except => [:signin, :richtest]
![Page 24: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/24.jpg)
Boston Computing Review 2006
View : Layout Sample<html><head> <title>Admin: <%= controller.action_name %></title> <%= stylesheet_link_tag 'scaffold' %></head><body>
<p style="color: green"><%= flash[:notice] %></p>
<%= @content_for_layout %>
</body></html>
![Page 25: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/25.jpg)
Boston Computing Review 2006
View : Object from Controller
<% for column in User.content_columns %><p> <b><%= column.human_name %>:</b> <%=h
@user.send(column.name) %></p><% end %>
<%= link_to 'Edit', :action => 'edit', :id => @user %> |
<%= link_to 'Back', :action => 'list' %>
![Page 26: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/26.jpg)
Boston Computing Review 2006
View : Navigation
<% for column in User.content_columns %><p> <b><%= column.human_name %>:</b> <%=h
@user.send(column.name) %></p><% end %>
<%= link_to 'Edit', :action => 'edit', :id => @user %> |
<%= link_to 'Back', :action => 'list' %>
![Page 27: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/27.jpg)
Boston Computing Review 2006
View : Forms
• Native validation based on model• Partials to separate code from main view
view _formname.rhtml
<h1>Quick Account Signup</h1>
<%= start_form_tag :action => 'create' %> <%= render :partial => 'formname' %> <%= submit_tag "Create" %><%= end_form_tag %>
![Page 28: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/28.jpg)
Boston Computing Review 2006
View : Forms<%= error_messages_for 'user' %>
<!--[form:user]--><p><label for="user_firstname">Firstname</label><br/><%= text_field 'user', 'firstname' %></p>
<p><label for="user_lastname">Lastname</label><br/><%= text_field 'user', 'lastname' %></p>
<p><label for="user_email">Email</label><br/><%= text_field 'user', 'email' %></p>
<p><label for="user_username">Username</label><br/><%= text_field 'user', 'username' %></p>
<p><label for="user_password">Password</label><br/><%= password_field 'user', 'password' %></p><br>
<!--[eoform:user]-->
![Page 29: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/29.jpg)
Boston Computing Review 2006
Scaffolding
• Fast– Famous video (blog in 15 min)
http://media.rubyonrails.org/video/rails_take2_with_sound.mov
• Dynamic view from DB
• Great starting place
• Needs more work
![Page 30: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/30.jpg)
Boston Computing Review 2006
Deploying
• Time consuming but straightforward
• Unix / Linux hosts
• TextDrive
• Lighhtpd server
![Page 31: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/31.jpg)
Boston Computing Review 2006
Miscellaneous / Thoughts
• Logging Framework Ready to Use
• Interpreted (no waiting for compile)
• Once you get the hang of Ruby a lot of fun to quickly develop
• In the weeds is still in the weeds
![Page 32: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/32.jpg)
Boston Computing Review 2006
Where to Look Next
• O’Reilly Onlamp - Great Starting Place– http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html– http://www.onlamp.com/pub/a/onlamp/2005/03/03/rails.html
• Books from 37 Signals– Agile Web Development With Rails– Rails Recipies– http://www.rubyonrails.org/docs
![Page 33: Boston Computing Review - Ruby on Rails](https://reader033.vdocuments.us/reader033/viewer/2022052900/555a446cd8b42a83368b5298/html5/thumbnails/33.jpg)
Boston Computing Review 2006
On the CD
• IDE – RadRails– Built on Eclipse
• Database – MySQL 5.0 with GUI Tools– Ever wonder why the SQL Server 2005 GUI looks like
it does? :)
• O’Reilly Onlamp Articles• Ruby on Rails Cheat Sheet• Famous Blog Video• This Powerpoint Deck