Author
newbu
View
736
Download
3
Embed Size (px)
DESCRIPTION
BCR : Ruby on Rails
an introduction
more at http://www.rubyonrails.org/
Background / Agenda
Upcoming web projectFearsWelcome RubyGetting Onto RailsFramework, MVC, Databases, Application Servers and moreDeploying RORWhere to look NextConsiderations
.NETExperienceJ2EEEnterprise AcceptancePHPProven, knowledgebaseRuby on RailsDevelopment Speed, native MVC *and a little fun*Fears
PHP vs RORStabilityLack of Public Knowledge BaseShark AttackMissing IntelisenseRuby
Based on SmallTalk, Perl, LispObject OrientedNot strongly typedBasics http://www.fincher.org/tips/Languages/Ruby/FUN? :)Rails Framework
Power Through Rules and Best PracticeMVCAssumes a DatabaseObject Relational MappingForms Handling Been there done thatParametersLink BuildingScaffoldingMVC
Nothing New, 1973ModelYour Data and Data RulesViewInterfaceControllerTraffic DirectorMVC in ROR
View
ShowUser.rhtml
User Name
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
Model
Object Relational MappingActiveRecordLess Database glue Code *sigh of relief!*Worst Case Scenario Optimizations Possible with manual SQLLogging for Performance CheckingModel : Rules
Table NamesPluralsAttribute Namesid for primary key in tabletable_id for foreign key in other tableAbility to run joins via objects!Article.User.UsernameLegacy Options AvailableModel : Sample from Text
Model : Sample from Text
Model : Sample from Text
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
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]
Controller
Method name matches view folderusers_controller.rb works for /views/users/***.rhtmlcalled actionsall views methods will sit thereAbility toCRUDFlashRedirectController : 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
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
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
Controller : Getting Data
Request Data (POST / GET)Params hashModelsSession dataetcViews
Show the dataTemplates (layouts)Use objects from controllerNavigate guide into controller / actionFormsViews : Showing Data
Inline Ruby (similar to JSP):
'edit', :id => @user %> |
'list' %>
Views : Layouts
Inherit by default for controllerException in controller#set the layout
layout "articles", :except => [:signin, :richtest]
View : Layout Sample
Admin:
View : Object from Controller
:
'edit', :id => @user %> |
'list' %>
View : Navigation
:
'edit', :id => @user %> |
'list' %>
View : Forms
Native validation based on modelPartials to separate code from main view view _formname.rhtmlQuick Account Signup
'create' %>
'formname' %>
View : Forms
Firstname
Lastname
Email
Username
Password
Scaffolding
FastFamous video (blog in 15 min) http://media.rubyonrails.org/video/rails_take2_with_sound.movDynamic view from DBGreat starting placeNeeds more workDeploying
Time consuming but straightforwardUnix / Linux hostsTextDriveLightHTTPD serverMySQLMiscellaneous / Thoughts
Logging Framework Ready to UseInterpreted (no waiting for compile)Once you get the hang of Ruby a lot of fun to quickly developIn the weeds is still in the weedsWhere to Look Next
OReilly Onlamp - Great Starting Placehttp://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.htmlhttp://www.onlamp.com/pub/a/onlamp/2005/03/03/rails.htmlBooks from 37 SignalsAgile Web Development With RailsRails Recipieshttp://www.rubyonrails.org/docsOn the CD
IDE RadRailsBuilt on EclipseDatabase MySQL 5.0 with GUI ToolsEver wonder why the SQL Server 2005 GUI looks like it does? :)OReilly Onlamp ArticlesRuby on Rails Cheat SheetFamous Blog VideoThis Powerpoint Deck