BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

1,258 views

Published on

1 Comment
3 Likes
Statistics
Notes
No Downloads
Views
Total views
1,258
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
38
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

  1. 1. Boston Computing Review 2006 BCR : Ruby on Rails an introduction more at http://www.rubyonrails.org/
  2. 2. 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
  3. 3. Boston Computing Review 2006 Considerations • .NET – Experience • J2EE – Enterprise Acceptance • PHP – Proven, knowledgebase • Ruby on Rails – Development Speed, native MVC *and a little fun*
  4. 4. Boston Computing Review 2006 Fears • PHP vs ROR • Stability • Lack of Public Knowledge Base • “Shark Attack” • Missing Intelisense…
  5. 5. Boston Computing Review 2006 Ruby • Based on SmallTalk, Perl, Lisp • Object Oriented • Not strongly typed • Basics http://www.fincher.org/tips/Languages/Ruby/ • “FUN”? :)
  6. 6. 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
  7. 7. Boston Computing Review 2006 MVC • Nothing New, 1973 • Model – Your Data and Data Rules • View – Interface • Controller – Traffic Director
  8. 8. Boston Computing Review 2006 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. 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
  10. 10. 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
  11. 11. Boston Computing Review 2006 Model : Sample from Text
  12. 12. Boston Computing Review 2006 Model : Sample from Text
  13. 13. Boston Computing Review 2006 Model : Sample from Text
  14. 14. Boston Computing Review 2006 Model : Code Sample class 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
  15. 15. Boston Computing Review 2006 Model : DB Logging Processing 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/search Rendering search/list Completed in 0.03100 (32 reqs/sec) | Rendering: 0.01500 (48%) | DB: 0.00000 (0%) | 200 OK [http://localhost/search/list]
  16. 16. 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
  17. 17. 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
  18. 18. 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
  19. 19. 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
  20. 20. Boston Computing Review 2006 Controller : Getting Data • Request Data (POST / GET) – Params hash • Models • Session data • etc
  21. 21. Boston Computing Review 2006 Views • Show the data • Templates (layouts) • Use objects from controller • Navigate guide into controller / action • Forms
  22. 22. 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' %>
  23. 23. Boston Computing Review 2006 Views : Layouts • Inherit by default for controller • Exception in controller #set the layout layout "articles", :except => [:signin, :richtest]
  24. 24. 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>
  25. 25. 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' %>
  26. 26. 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' %>
  27. 27. 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 %>
  28. 28. 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]-->
  29. 29. Boston Computing Review 2006 Scaffolding • Fast – Famous video (blog in 15 min) http://media.rubyonrails.org/video/rails_take2_with • Dynamic view from DB • Great starting place • Needs more work
  30. 30. Boston Computing Review 2006 Deploying • Time consuming but straightforward • Unix / Linux hosts • TextDrive • LightHTTPD server • MySQL
  31. 31. 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
  32. 32. 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
  33. 33. 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

×