Rails Chronicle

Embed Size (px)

DESCRIPTION

form_forすごい。超すごい

Text of Rails Chronicle

  • 2008 / 2 / 16PostgreSQL / Ruby

    / Rails@

    Rails Chroniclehistory of name attribute and form_* helper methods.

  • ()

    ()

    RubyRails

    Rails@

  • Acknowledgments

    PostgreSQL /

    Ruby

    Rails@

  • Acknowledgments

  • ()Rails

    Rails

    Ruby

  • Contact me

    k-morohashi@esm.co.jp (work)moronatural@gmail.com (private)http://d.hatena.ne.jp/moro/

    :- )

  • blogFeedList

    http://feedlist.net/

  • Conclusion

    # in app/controllers/entries_controller.rbentry = Entry.new(params[:entry])

    /

    form

  • What is ?

  • is an HTML element.

    Title

    Body

    form

    HTTP

  • is an Interface.

    User Interface.

  • has some Controls.Control(input or so)

    Controlname

    Title

    Body

  • What is controls

    name attribute. name

    name

  • name attribute is the Programming Interface

    formAPI!nameAPI!

    actionmethod

  • is an Interface.

    User Interface

    Programing Interface

  • How does Rails handle?

  • Phase 1

    From View to Controller

  • Rails handle query parameters

    like a Hash

    POST /entry entry[title]=new title&entry[body]=new body

    RailsHash

    params() # =>{ :entry => {:title => new title, :body => new body}}

  • Examples

    {:title => new title, :body => new body}

  • Examples

    { :entry => {:title => new title, :body => new body }}

  • Examples

    {"entry"=>{"title"=>"aaa", "links"=>["xxx", "yyy", "zzz"]}}

  • Phase 2

    From Controller to Model (and DB)

  • Rails handle Hash as

    AR constructor argument.

    RailsHashActiveRecord

    hash = { :entry => {:title => new title, :body => new body}}

    entry = Entry.new(hash[:entry])entry.title # => new title

  • And, of course, Rails can

    save AR object to DB

    RailsActiveRecordDB

    entry = Entry.new(hash)entry.title # => new title

    entry.save

  • Therefore Rails can

    save form-data to DB at ease.

    RailsformDB

    entry = Entry.new(params[:entry])

    entry.save

  • In other words,

    params[]

    formname

    entry = Entry.new(params[:entry])

  • history of name attribute andform_* helper methods.

    Rails Chronicle

  • ~ Rails 1.0Before form_for()

  • form_tag(url_for_options = {}, options = {}, *parameters_for_url)

    Rails 1.0

    HTMLform actionurl_for_options

    options

  • Thin wrapper for HTML tag.

    form_tag( {:controller=>entries,:action=>create}, {:id=>new_entry, :method=>post} )

    entries/create>

    generates

  • andthere is lovely end_form_tag()

    # Outputs def end_form_tag end

  • ActionView::Helpers::

    FormHelper.

    FormHelper!!@entry = Entry.new(:title=>)text_field(:entry, :title)

    # =>

    ActiveRecord(=DB)form

  • ActionView::Helpers::

    FormHelper.@entry = Entry.new(:title=>)text_field(:entry, :title)

    # =>

    {:entry => {:title => }}

    and you will get

  • ActionView::Helpers::

    FormHelper.

    text_field(object, method, options={})

    object @#{object}

    method

    #{object}[#{name}]nameinput

  • Examples

    { :entry => {:title => new title, :body => new body }}

    remind

  • Rails 1.1& Rails1.2

    form_for() has come.

  • form_for( object_name, *args, &proc)

    Rails 1.1

    URL

    Creates a form and a scope around a specific model object, (from API Document)

  • form for the object.

    {:action=>crate}) do |f| %> ...

    text_field()

    ==

  • form_for() remembers the object.

    {:action=>crate}) do |f| %> # @entry#titletext field Title

    ActionView::FormBuilder

    text_field(method, options={}) cf : text_field(object, method, options={}) in Rails 1.0

  • ActionView::

    FormBuilder{:action=>crate}) do |f| %> Title

    Title

    generates

  • local) %>

    {:action=>crate}) do |f| %> Title

    FYI: 2nd argument.

    respond_to?(:title)OK

  • OK, but

    Why form_for()? RailsHash

    Creates a form and a scope around a specific model object,

    CRUDHTML

    remind

    remind

  • OK, but

    Does it works well?

    has_many :through CRUD

    RDBMS

  • But... form_for

    (Rails@)

    end_form_tag()obsolete

    (?)

  • Rails 2.0

    form_for() meets Resource

  • form_for() and map.resource

    ActionController::Routes.draw do |m| map.resources :entriesend

    AR resourceresourceHTTPCRUD

  • new form_for()

    ...

    ...

    generates

  • ...

    ActiveRecordConvention

    classID

    form_for() determine the form identity.

  • new Convention forDOM ID & CSS class

    CSS classAR IDAR_DBID prefixeditnew

    classIDdom_class()dom_id()

  • form_for() remembers the object.

    Title

    remind

    FormBuilder

  • ActionView::

    FormBuilder#label

    {:action=>crate}) do |f| %>

    label(method, text=nil, options={})

    ......

  • AR

    POST /entriesPUT /entries/:id

    form_for() understands the objects status.

    ...

  • bigger

    Convention for CRUD

    HTTPCRUDConventionRFC2616

    Rails

  • on RubyKaigi2006,

    DHH saidGET POST PUT DELETE

    find create update destroy

    SELECT INSERT UPDATE DELETE

    http://media.rubyonrails.org/presentations/worldofresources.pdf

  • HTTP methods and actionsGET /entries/:id {:controller=>"entries", :action=>"show"}

    PUT /entries/:id {:controller=>"entries", :action=>"update"}

    DELETE /entries/:id {:controller=>"entries", :action=>"destroy"}

    POST /entries{:controller=>"entries", :action=>"create"}

  • HTTP Method!

    HTTP Method

    !

    CRUD

    ActiveRecord!

    ActiveRecordDBCRUD!!

    DBCRUDform_for

    resource orientedform....

    HTTPCRUD

  • 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 endend

  • 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 endend

    def show @entry = Entry.find(params[:id]) end

  • 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 endend

    def create @entry = Entry.new(params[:entry]) @entry.saveend

  • 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 endend

    def update @entry = Entry.find(params[:id]) @entry.update_attributes(params[:entry])end

  • 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 endend

    def destroy @entry = Entry.find(params[:id]) @entry.destroyend

  • FYI: about PUT and DELETE

    ...

    PUT

    POST

    _method

  • Ive talked about

    features of form_for()w/ Rails 2.0

    formDOM IDCSS

    FormBuilder

    remind

  • One more(sad) thing..

  • undefined local v

    ariable or

    method `end_form

    _tag

    Good bye end_form_tag()

  • Conclusion

  • the Goalentry = Entry.new(params[:entry])

    entry.save()

  • Where form_for() and AC::Resource going to?

    WebCRUD

    relationshipCRUD has_many :through

  • Conclusion of Conclusions

    Rails 2.0form_for()AC::Resources

    Railsblog2.0!!

    &

  • Any Question?

  • FAQ:

    ActiveResource? ?

    RESTfulWebARes

    RailsActionControllerARes

    ActionController::Resouces

  • FAQ: +

    ?

    Rails 1.2DOM ID

    1.2URL

  • FAQ:

    form_for()?

    config/routes.rbresourcemap.resources reso