33
Boston Computing Review 2006 BCR : Ruby on Rails an introduction more at http://www.rubyonrails.org/

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

  • Upload
    newbu

  • View
    757

  • Download
    3

Embed Size (px)

DESCRIPTION

 

Citation preview

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

Boston Computing Review 2006

BCR : Ruby on Rails

an introductionmore at http://www.rubyonrails.org/

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

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

Boston Computing Review 2006

Fears

• PHP vs ROR

• Stability

• Lack of Public Knowledge Base

• “Shark Attack”

• Missing Intelisense…

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

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

Boston Computing Review 2006

MVC

• Nothing New, 1973

• Model– Your Data and Data Rules

• View– Interface

• Controller– Traffic Director

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

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

Boston Computing Review 2006

Model : Sample from Text

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

Boston Computing Review 2006

Model : Sample from Text

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

Boston Computing Review 2006

Model : Sample from Text

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

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

Boston Computing Review 2006

Controller : Getting Data

• Request Data (POST / GET)– Params hash

• Models

• Session data

• etc

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

Boston Computing Review 2006

Views

• Show the data

• Templates (layouts)

• Use objects from controller

• Navigate guide into controller / action

• Forms

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

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

Boston Computing Review 2006

Views : Layouts

• Inherit by default for controller

• Exception in controller#set the layout

layout "articles", :except => [:signin, :richtest]

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

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

Boston Computing Review 2006

Deploying

• Time consuming but straightforward

• Unix / Linux hosts

• TextDrive

• LightHTTPD server

• MySQL

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

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

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: BCR Ruby on Rails - Boston Computing Review : Ruby on Rails

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