crud presentation
TRANSCRIPT
-
8/14/2019 CRUD presentation
1/63
CRUDNow This App Is Totally FUCD
-
8/14/2019 CRUD presentation
2/63
CRUD is a way ofthinking.
-
8/14/2019 CRUD presentation
3/63
Create
ReadUpdate
Delete
-
8/14/2019 CRUD presentation
4/63
-
8/14/2019 CRUD presentation
5/63
The Seven CRUD
Methods
deflist; end
defshow; enddefnew; enddefcreate; enddefedit; end
defupdate; enddefdestroy; end
# Show all items
# Show a specific item# Form for creating new item# Create new item# Form for editing new item
# Update and existing item# Destroy an existing item
-
8/14/2019 CRUD presentation
6/63
Those are your verbs.Welcome to a world of
nouns.
-
8/14/2019 CRUD presentation
7/63
Get every controller tomatch those methods,
and youre done.
-
8/14/2019 CRUD presentation
8/63
OK, Maybe not all willmatch. Some things justcant be CRUDy well.
-
8/14/2019 CRUD presentation
9/63
Making CRUDFrom the Ground On Up
-
8/14/2019 CRUD presentation
10/63
Show and Make Thingsdefindex @things = Thing.find_allend
defnew @thing = Thing.newend
defcreate @thing = Thing.create(params[:thing]) redirect_to:action => :indexend
-
8/14/2019 CRUD presentation
11/63
index.rhtml thing, :collection => @things %>
new.rhtml { :action => :create } do %>
Name:
-
8/14/2019 CRUD presentation
12/63
-
8/14/2019 CRUD presentation
13/63
edit.rhtml { :action => :update} do %>Name:
-
8/14/2019 CRUD presentation
14/63
Get Rid of Things
defdestroy
@thing = Thing.find(params[:id]) @thing.destroyredirect_to:action => :index
end
-
8/14/2019 CRUD presentation
15/63
This is Basic Rails
-
8/14/2019 CRUD presentation
16/63
Next is Where ThingsStart To Get Messy
-
8/14/2019 CRUD presentation
17/63
Adding Categoriesclass Thing < ActiveRecord::Base has_and_belongs_to_many:categories
end
class Category < ActiveRecord::Base has_and_belongs_to_many:thingsend
-
8/14/2019 CRUD presentation
18/63
How Do We Add Thingsto Categories?
-
8/14/2019 CRUD presentation
19/63
Put It In ThingsController!defadd_to_category @thing = Thing.find(params[:thing_id]) @category = Category.find(params[:category_id]) @thing.categories :indexend
defremove_from_category
@thing = Thing.find(params[:id]) @category = Category.find(params[:category_id]) @thing.delete(@category) redirect_to:action => :indexend
-
8/14/2019 CRUD presentation
20/63
This Is Not CRUD!
-
8/14/2019 CRUD presentation
21/63
Instead of shoehorningthis in, try to ensureyour app is CRUD.
-
8/14/2019 CRUD presentation
22/63
CRUD Tip #1Many-to-Many Associations need CRUD and new
models.
-
8/14/2019 CRUD presentation
23/63
Again, with CRUDclass Thing < ActiveRecord::Base has_many:categorizationshas_many:categories, :through => :categorizations
end
class Category < ActiveRecord::Base has_many:categorizationshas_many:things, :through => :categorizations
end
class Categorization < ActiveRecord::Base belongs_to:thing belongs_to:category
end
-
8/14/2019 CRUD presentation
24/63
The Controllerclass CategorizationsController < ApplicationControllerdefcreate
@categorization = Categorization.create(params[:categorization] )
redirect_to:controller => things, :action => indexend
defdestroy
@categorization = Categorization.find(params[:id]) @categorization.destroyredirect_to:controller => things, :action => index
endend
-
8/14/2019 CRUD presentation
25/63
Whats the big deal?
These are basically thesame methods in a
different place!
-
8/14/2019 CRUD presentation
26/63
FALSE!
-
8/14/2019 CRUD presentation
27/63
The new methods arecleaner.
-
8/14/2019 CRUD presentation
28/63
The architecture iseasier to read.
-
8/14/2019 CRUD presentation
29/63
The association cannow have meaningful
properties.
-
8/14/2019 CRUD presentation
30/63
OK, associations are aprime place for CRUD.Is that all it helps with?
-
8/14/2019 CRUD presentation
31/63
Take a look at AJAX.
-
8/14/2019 CRUD presentation
32/63
Creating with AJAX
defcreate_with_ajax
@categorization = Categorization.create(params[:categorization] )
end
-
8/14/2019 CRUD presentation
33/63
Weve Seen This Beforedefcreate
@categorization = Categorization.create(
params[:categorization] ) redirect_to:controller => things, :action => indexend
defcreate_with_ajax
@categorization = Categorization.create(params[:categorization] )
end
-
8/14/2019 CRUD presentation
34/63
Duplication is bad, mkay.
-
8/14/2019 CRUD presentation
35/63
So, lets get back toCRUD.
-
8/14/2019 CRUD presentation
36/63
Now With respond_to
defcreate @categorization = Categorization.create(params[:categorization] )
respond_to do |type|type.html { redirect_to:controller => things , :action => index }type.js { render }
end
end
-
8/14/2019 CRUD presentation
37/63
CRUD Tip #2Use respond_to for controlling view selection.
-
8/14/2019 CRUD presentation
38/63
CRUD help anywhereelse?
-
8/14/2019 CRUD presentation
39/63
Lets add users and thensee.
-
8/14/2019 CRUD presentation
40/63
AccountController
deflogin; enddeflogout; end
defsignup; enddefedit; end
# CREATE a login# DESTROY a login
# CREATE a user# UPDATE a user
The comments say it all.
-
8/14/2019 CRUD presentation
41/63
CRUD Tip #3Events are a great place to insert CRUD.
-
8/14/2019 CRUD presentation
42/63
LoginControllerdefcreate @login=Login.create(params[:login])end
defdestroy @login = Login.find(params[:id]) @login.destroy
end
-
8/14/2019 CRUD presentation
43/63
The Modelsclass Event < ActiveRecord::Baseend
class Login < Event belongs_to:userend
class User < ActiveRecord::Base has_many :loginsend
-
8/14/2019 CRUD presentation
44/63
Hey Idiot, I just made amodel for Logins. Argh,
CRUD wastes time!
-
8/14/2019 CRUD presentation
45/63
Actually, look at whatyoure now able to do.
-
8/14/2019 CRUD presentation
46/63
LoginController
separates concept oflogging in from User
management.
-
8/14/2019 CRUD presentation
47/63
Login model allows usto track the users logins
over time.
-
8/14/2019 CRUD presentation
48/63
Modeling events givesyou information about
user behavior.
-
8/14/2019 CRUD presentation
49/63
Knowing your usersmakes you money!
-
8/14/2019 CRUD presentation
50/63
Going from RPC to REST, I cut
the total number of actions byalmost twenty.- Scott Raymond, IconBuffet
-
8/14/2019 CRUD presentation
51/63
CRUD saves youmoney!
-
8/14/2019 CRUD presentation
52/63
Its got other benefits,too.
-
8/14/2019 CRUD presentation
53/63
simply_restfulA plugin for implementing verb oriented controllers.
-
8/14/2019 CRUD presentation
54/63
Current URLs
deflist; end
defshow; enddefnew; enddefcreate; enddefedit; end
defupdate; enddefdestroy; end
# GET things/list
# GET things/show/1# GET things/new# POST things/create# GET things/edit/1
# POST things/update/1# POST things/destroy/1
-
8/14/2019 CRUD presentation
55/63
simply_restful URLs
deflist; end
defshow; enddefnew; enddefcreate; enddefedit; end
defupdate; enddefdestroy; end
# GET things
# GET things/1# GET things;new# POST things# GET things/1;edit
# PUT things/1# DELETE things/1
-
8/14/2019 CRUD presentation
56/63
Who cares? Its just aURL.
-
8/14/2019 CRUD presentation
57/63
Its prettier.
(And it makes betteruse of HTTP)
-
8/14/2019 CRUD presentation
58/63
It gives youActiveResource!
-
8/14/2019 CRUD presentation
59/63
ActiveResource is aneasy to use API in
development by DHH.Look at the source code or DHHs presentation tolearn more.
-
8/14/2019 CRUD presentation
60/63
Recap
-
8/14/2019 CRUD presentation
61/63
CRUD is an
architecture that helpsyou organize your code
and remove obfuscation.
-
8/14/2019 CRUD presentation
62/63
CRUD helps you makemoney!
-
8/14/2019 CRUD presentation
63/63
Other Resources
DHHs presentation: www.loudthinking.com/lt-files/worldofresources.pdf Scott Raymonds blog: scottraymond.net/ Ryan Daigles article: www.ryandaigle.com/articles/
2006/06/30/whats-new-in-edge-rails-activeresource-is-
here