Rails and Ajax - PowerPoint PPT Presentation

About This Presentation
Title:

Rails and Ajax

Description:

Form data is sent as a URL with ?form_data info appended to the end. Can be used only if data is all ASCII and not more than 100 characters. method='post' ... – PowerPoint PPT presentation

Number of Views:164
Avg rating:3.0/5.0
Slides: 15
Provided by: cisU
Category:

less

Transcript and Presenter's Notes

Title: Rails and Ajax


1
Rails and Ajax
2
HTML Forms
  • The ltform argumentsgt ... lt/formgt tag encloses
    form elements (and usually includes other HTML as
    well)
  • The arguments to form tell what to do with the
    user input
  • actionURL" (required)
  • Specifies where to send the data when the Submit
    button is clicked
  • method"get" (default)
  • Form data is sent as a URL with ?form_data info
    appended to the end
  • Can be used only if data is all ASCII and not
    more than 100 characters
  • method"post"
  • Form data is sent in the body of the URL request
  • Cannot be bookmarked by most browsers
  • target"target"
  • Tells where to open the page sent as a result of
    the request
  • target _blank means open in a new window
  • target _top means use the same window

3
Ruby forms
  • In a template, you should use form_tag() instead
    of ltformgt
  • The first argument is a hash that tells which
    action to invoke
  • This takes the same options as url_for() see
    next slide
  • The optional second argument is another hash for
    setting HTML form attributes
  • Example lt form_tag actiongtsave ,
    classgt"compact" gt lt end_form_tag gt

4
url_for()
  • The purpose of url_for is to avoid the need to
    write hardcoded URLs
  • url_for takes a hash of options as its parameter
  • Options include controller, action, and
    possibly id
  • Reasonable defaults are used
  • You can define other options with a map.connect
    call in config/routes.rb (see your textbook for
    details)
  • Example url_for(controller gt "store",
    action gt "list")translates to
    http//your_application/store/list

5
Field helpers (for models)
  • Rails provides support (in the app/helpers
    directory) for HTML fields
  • text_field(variable, attribute, options)
  • hidden_field(variable, attribute, options)
  • password_field(variable, attribute, options)
  • text_area(variable, attribute, options)
  • radio_button(variable, attribute, tag_value,
    options)
  • check_box(variable, attribute, options,
    on_value, off_value)
  • select(variable, attribute, choices, options,
    html_options)
  • The first argument to a helper method is the name
    of an instance variable (typically a model
    object)
  • The second argument is an attribute of that
    object
  • The first and second arguments are combined (with
    an underscore) to form the id of the generated
    field
  • Other arguments depend on the field type
  • For text fields, the third argument is a hash of
    options, for example, size

6
Field helpers (for nonmodels)
  • Rails can also support fields that have no
    corresponding models
  • Names end in _tag
  • First argument is a simple name, not an object
  • Example syntax text_field_tag(name, value
    nil, options )
  • Example text_field_tag(arg1, _at_paramsarg1,
    size gt 12)
  • Values will be stored in the params hash when the
    form is submitted to the controller

7
Links
  • The basic HTML link is lta href"URL"gt
  • The basic RHTML link is lt link_to("Link
    text", action gt "method_name") gtwhere
    method_name uses the url_for() format
  • The basic Ajax/RHTML link is lt
    link_to_remote( "Link text",
    update gt id_of_element_to_update,
    url gt action gt method_name ) gtwhich
    refers to ltdiv id" id_of_element_to_update
    "gtOriginal textlt/divgt

8
Responding to an Ajax link
  • The method_name in the link_to_remote is the name
    of a method in the controller
  • Since we do not want to return a complete HTML
    page, the named method should contain at least
    the following render(layout gt false)
  • Whatever is in the corresponding .rhtml will
    replace the original contents of the ltdivgt
    element

9
form_remote_tag()
  • To use Ajax for any form, replace form_tag and
    end_form_tag with form_remote_tag and
    end_form_tag
  • All form elements will be serialized and sent to
    the server
  • They can be read from the params hash

10
Observers
  • Observers let you call Ajax actions when the user
    changed data on a form
  • Example (p. 392 in textbook)
  • ltlabel for"search"gtSearch termlt/labelgtlt
    text_field_tag search gtlt observe_field(searc
    h, frequency gt 0.5,
    update gt results,
    url gt action gt
    search ) gtltdiv id"results"gtlt/divgt

11
CRUD
  • Basic database operations are Create, Read,
    Update, and Delete
  • The scaffold method does a lot of the database
    work for you
  • Scaffolds are intended to be replaced
  • ActiveRecordBase provides all your basic needs
  • An ActiveRecord wraps a database changes to the
    ActiveRecord result in changes to the database
  • Each ActiveRecord object corresponds to a row in
    the database
  • The attributes of the object correspond to the
    column in the database

12
Some database methods
  • new(attributes nil)
  • Creates a record with the given attributes
  • find(args)
  • find by id
  • Person.find(1) returns the object for ID 1
  • find first
  • Person.find(first) returns the first object
    fetched by SELECT FROM people
  • find all
  • Person.find(all) returns an array of objects
    for all the rows
    fetched by SELECT FROM people
  • update(id, attributes)
  • Creates a record with the given attributes
  • destroy(id)

13
References
  • Agile Web Development with Rails, by Dave Thomas
    and David Hansson
  • The Ajax material is from chapter 18, The Web,
    V2.0
  • Some of the forms information is from chapters 16
    and 17
  • Database material from chapter 14, especially
    14.5
  • api.rubyonrails.com
  • Database examples from ActiveRecordBase
  • Some other examples from individual methods

14
The End
Write a Comment
User Comments (0)
About PowerShow.com