47. form_for() and map.resource
• AR ”resource”
•“resource” HTTP CRUD
ActionController::Routes.draw do |m|
map.resources :entries
end
48. new form_for()
<% @entry = Entry.find(1) %>
<% form_for(@entry) do |f|%>
...
<% end %>
generates
<form action=quot;/entries/1quot; class=quot;edit_entryquot;
id=quot;edit_entry_1quot; method=quot;postquot;>
<input name=quot;_methodquot; type=quot;hiddenquot; value=quot;putquot; />
...
</form>
49. form_for()
determine the form identity.
<form action=quot;/entries/1quot; class=quot;edit_entryquot;
id=quot;edit_entry_1quot; method=quot;postquot;>
<input name=quot;_methodquot; type=quot;hiddenquot; value=quot;putquot; />
...
</form>
• ActiveRecord
Convention
• class ID
50. new Convention for
DOM ID & CSS class
<form action=quot;/entries/1quot; class=quot;edit_entryquot;
id=quot;edit_entry_1quot; method=quot;postquot;>
• CSS class ” AR ”
• ID ” AR _ DB ID ”
• prefix ”edit” ”new”
• class ID dom_class() dom_id()
51. d
re min form_for()
remembers the object.
• FormBuilder
<% form_for(@entry) do |f| %>
<label>Title</label>
<%= f.text_field :title %>
<% end %>
55. on RubyKaigi2006,
DHH said
GET POST PUT DELETE
find create update destroy
SELECT INSERT UPDATE DELETE
http://media.rubyonrails.org/presentations/worldofresources.pdf
56. HTTP methods and actions
GET /entries/:id
{:controller=>quot;entriesquot;, :action=>quot;showquot;}
PUT /entries/:id
{:controller=>quot;entriesquot;, :action=>quot;updatequot;}
DELETE /entries/:id
{:controller=>quot;entriesquot;, :action=>quot;destroyquot;}
POST /entries
{:controller=>quot;entriesquot;, :action=>quot;createquot;}
57. HTTP Method !
HTTP Method
!
CRUD
ActiveRecord !
ActiveRecord DB CRUD !!
DB CRUD form_for
resource oriented form ....
HTTP CRUD
58.
59. class EntriesController < ApplicationController
def show
@entry = Entry.find(params[:id])
end
def create
@entry = Entry.new(params[:entry])
@entry.save
end
def update
@entry = Entry.find(params[:id])
@entry.update_attributes(params[:entry])
end
def destroy
@entry = Entry.find(params[:id])
@entry.destroy
end
end
60. class EntriesController < ApplicationController
def show
@entry = Entry.find(params[:id])
end
def create
@entry = Entry.new(params[:entry])
@entry.save
def show
end
@entry = Entry.find(params[:id])
def update
end @entry = Entry.find(params[:id])
@entry.update_attributes(params[:entry])
end
def destroy
@entry = Entry.find(params[:id])
@entry.destroy
end
end
61. class EntriesController < ApplicationController
def show
@entry = Entry.find(params[:id])
end
def create
@entry = Entry.new(params[:entry])
@entry.save
def create
end
@entry = Entry.new(params[:entry])
@entry.save
def update
@entry = Entry.find(params[:id])
end
@entry.update_attributes(params[:entry])
end
def destroy
@entry = Entry.find(params[:id])
@entry.destroy
end
end
62. class EntriesController < ApplicationController
def show
@entry = Entry.find(params[:id])
end
def create
@entry = Entry.new(params[:entry])
@entry.save
def update
end
@entry = Entry.find(params[:id])
@entry.update_attributes(params[:entry])
def update
@entry = Entry.find(params[:id])
end
@entry.update_attributes(params[:entry])
end
def destroy
@entry = Entry.find(params[:id])
@entry.destroy
end
end
63. class EntriesController < ApplicationController
def show
@entry = Entry.find(params[:id])
end
def create
@entry = Entry.new(params[:entry])
@entry.save
def destroy
end
@entry = Entry.find(params[:id])
@entry.destroy
def update
@entry = Entry.find(params[:id])
end
@entry.update_attributes(params[:entry])
end
def destroy
@entry = Entry.find(params[:id])
@entry.destroy
end
end
64. FYI: about PUT and DELETE
• PUT
• POST
• _method
<form action=quot;/entries/1quot; class=quot;edit_entryquot;
id=quot;edit_entry_1quot; method=quot;postquot;>
<input name=quot;_methodquot; type=quot;hiddenquot; value=quot;putquot; />
...
</form>
65. rem
ind I’ve talked about
features of form_for()w/ Rails 2.0
• form DOM ID
CSS
•
FormBuilder
•