Boston Computing Review - Ruby on Rails

3,064 views

Published on

This presentation provides a high level overview of the Ruby on Rails development framework.

Published in: Technology
2 Comments
2 Likes
Statistics
Notes
No Downloads
Views
Total views
3,064
On SlideShare
0
From Embeds
0
Number of Embeds
34
Actions
Shares
0
Downloads
32
Comments
2
Likes
2
Embeds 0
No embeds

No notes for slide

Boston Computing Review - Ruby on Rails

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

×