Your SlideShare is downloading. ×
(Powerpoint) - 14 slides
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

(Powerpoint) - 14 slides


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Rails and Ajax
  • 2. HTML Forms
    • The <form arguments > ... </form> tag encloses form elements (and usually includes other HTML as well)
    • The arguments to form tell what to do with the user input
      • action=“ URL &quot; (required)
        • Specifies where to send the data when the Submit button is clicked
      • method=&quot;get&quot; (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=&quot;post&quot;
        • Form data is sent in the body of the URL request
        • Cannot be bookmarked by most browsers
      • target=&quot; target &quot;
        • 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 <form>
      • 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: <%= form_tag { :action=>:save }, { :class=>&quot;compact&quot; } %> … <%= end_form_tag %>
  • 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 => &quot;store&quot;, :action => &quot;list&quot;) 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: t ext_field_tag(:arg1, @params[:arg1], :size => 12)
    • Values will be stored in the params hash when the form is submitted to the controller
  • 7. Links
    • The basic HTML link is <a href=&quot; URL &quot;>
    • The basic RHTML link is <%= link_to(&quot; Link text &quot;, :action => &quot; method_name &quot;) %> where method_name uses the url_for() format
    • The basic Ajax/RHTML link is <%= link_to_remote( &quot; Link text &quot;, :update => id_of_element_to_update , :url => { :action => : method_name } ) %> which refers to <div id=&quot; id_of_element_to_update &quot;> Original text </div>
  • 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 => false)
      • Whatever is in the corresponding .rhtml will replace the original contents of the <div> 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):
      • <label for=&quot;search&quot;>Search term:</label> <%= text_field_tag :search %> <%= observe_field(:search, :frequency => 0.5, :update => :results, :url => { :action => :search }) %> <div id=&quot;results&quot;></div>
  • 11. CRUD
    • Basic database operations are C reate, R ead, U pdate, and D elete
      • The :scaffold method does a lot of the database work for you
      • Scaffolds are intended to be replaced
    • ActiveRecord::Base 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
      • Database examples from ActiveRecord::Base
      • Some other examples from individual methods
  • 14. The End