Boston Computing Review : Ruby on Rails - Meetup

tunisianbromidrosisInternet and Web Development

Feb 5, 2013 (4 years and 4 months ago)

104 views

Boston Computing Review 2006

BCR :

Ruby on Rails

an introduction

more at
http://www.rubyonrails.org/


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

Boston Computing Review 2006

Considerations


.NET


Experience


J2EE


Enterprise Acceptance


PHP


Proven, knowledgebase


Ruby on Rails


Development Speed, native MVC

*and a little
fun*

Boston Computing Review 2006

Fears


PHP vs ROR


Stability


Lack of Public Knowledge Base


“Shark Attack”


Missing Intelisense…

Boston Computing Review 2006

Ruby


Based on SmallTalk, Perl, Lisp


Object Oriented


Not strongly typed


Basics
http://www.fincher.org/tips/Languages/Ruby/


“FUN”? :)



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



Boston Computing Review 2006

MVC


Nothing New, 1973


Model


Your Data and Data Rules


View


Interface


Controller


Traffic Director

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








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

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


Boston Computing Review 2006

Model :

Sample from Text

Boston Computing Review 2006

Model :

Sample from Text

Boston Computing Review 2006

Model :

Sample from Text

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

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]

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


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

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

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

Boston Computing Review 2006

Controller :
Getting Data


Request Data (POST / GET)


Params hash


Models


Session data


etc


Boston Computing Review 2006

Views


Show the data


Templates (layouts)


Use objects from controller


Navigate guide into controller / action


Forms


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' %>


Boston Computing Review 2006

Views :
Layouts


Inherit by default for controller


Exception in controller

#set the layout

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


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>

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' %>


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' %>


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 %>

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]
--
>

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

Boston Computing Review 2006

Deploying


Time consuming but straightforward


Unix / Linux hosts


TextDrive


LightHTTPD server


MySQL


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


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



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