Your SlideShare is downloading. ×
0
(Powerpoint) - 14 slides
(Powerpoint) - 14 slides
(Powerpoint) - 14 slides
(Powerpoint) - 14 slides
(Powerpoint) - 14 slides
(Powerpoint) - 14 slides
(Powerpoint) - 14 slides
(Powerpoint) - 14 slides
(Powerpoint) - 14 slides
(Powerpoint) - 14 slides
(Powerpoint) - 14 slides
(Powerpoint) - 14 slides
(Powerpoint) - 14 slides
(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

206

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
206
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Rails and Ajax
  • 2. HTML Forms <ul><li>The <form arguments > ... </form> tag encloses form elements (and usually includes other HTML as well) </li></ul><ul><li>The arguments to form tell what to do with the user input </li></ul><ul><ul><li>action=“ URL &quot; (required) </li></ul></ul><ul><ul><ul><li>Specifies where to send the data when the Submit button is clicked </li></ul></ul></ul><ul><ul><li>method=&quot;get&quot; (default) </li></ul></ul><ul><ul><ul><li>Form data is sent as a URL with ?form_data info appended to the end </li></ul></ul></ul><ul><ul><ul><li>Can be used only if data is all ASCII and not more than 100 characters </li></ul></ul></ul><ul><ul><li>method=&quot;post&quot; </li></ul></ul><ul><ul><ul><li>Form data is sent in the body of the URL request </li></ul></ul></ul><ul><ul><ul><li>Cannot be bookmarked by most browsers </li></ul></ul></ul><ul><ul><li>target=&quot; target &quot; </li></ul></ul><ul><ul><ul><li>Tells where to open the page sent as a result of the request </li></ul></ul></ul><ul><ul><ul><li>target = _blank means open in a new window </li></ul></ul></ul><ul><ul><ul><li>target = _top means use the same window </li></ul></ul></ul>
  • 3. Ruby forms <ul><li>In a template, you should use form_tag() instead of <form> </li></ul><ul><ul><li>The first argument is a hash that tells which action to invoke </li></ul></ul><ul><ul><ul><li>This takes the same options as url_for() – see next slide </li></ul></ul></ul><ul><ul><li>The optional second argument is another hash for setting HTML form attributes </li></ul></ul><ul><li>Example: <%= form_tag { :action=>:save }, { :class=>&quot;compact&quot; } %> … <%= end_form_tag %> </li></ul>
  • 4. url_for() <ul><li>The purpose of url_for is to avoid the need to write hardcoded URLs </li></ul><ul><li>url_for takes a hash of options as its parameter </li></ul><ul><li>Options include :controller , :action , and possibly :id </li></ul><ul><ul><li>Reasonable defaults are used </li></ul></ul><ul><ul><li>You can define other options with a map.connect call in config/routes.rb (see your textbook for details) </li></ul></ul><ul><li>Example: url_for(:controller => &quot;store&quot;, :action => &quot;list&quot;) translates to http:// your_application /store/list </li></ul>
  • 5. Field helpers (for models) <ul><li>Rails provides support (in the app/helpers directory) for HTML fields </li></ul><ul><ul><li>text_field(:variable, :attribute, options) </li></ul></ul><ul><ul><li>hidden_field(:variable, :attribute, options) </li></ul></ul><ul><ul><li>password_field(:variable, :attribute, options) </li></ul></ul><ul><ul><li>text_area(:variable, :attribute, options) </li></ul></ul><ul><ul><li>radio_button(:variable, :attribute, tag_value, options) </li></ul></ul><ul><ul><li>check_box(:variable, :attribute, options, on_value, off_value) </li></ul></ul><ul><ul><li>select(:variable, :attribute, choices, options, html_options) </li></ul></ul><ul><li>The first argument to a helper method is the name of an instance variable (typically a model object) </li></ul><ul><li>The second argument is an attribute of that object </li></ul><ul><ul><li>The first and second arguments are combined (with an underscore) to form the id of the generated field </li></ul></ul><ul><li>Other arguments depend on the field type </li></ul><ul><ul><li>For text fields, the third argument is a hash of options, for example, :size </li></ul></ul>
  • 6. Field helpers (for nonmodels) <ul><li>Rails can also support fields that have no corresponding models </li></ul><ul><ul><li>Names end in _tag </li></ul></ul><ul><ul><li>First argument is a simple name, not an object </li></ul></ul><ul><ul><li>Example syntax: text_field_tag(name, value = nil, options = { }) </li></ul></ul><ul><ul><li>Example: t ext_field_tag(:arg1, @params[:arg1], :size => 12) </li></ul></ul><ul><li>Values will be stored in the params hash when the form is submitted to the controller </li></ul>
  • 7. Links <ul><li>The basic HTML link is <a href=&quot; URL &quot;> </li></ul><ul><li>The basic RHTML link is <%= link_to(&quot; Link text &quot;, :action => &quot; method_name &quot;) %> where method_name uses the url_for() format </li></ul><ul><li>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> </li></ul>
  • 8. Responding to an Ajax link <ul><li>The method_name in the link_to_remote is the name of a method in the controller </li></ul><ul><ul><li>Since we do not want to return a complete HTML page, the named method should contain at least the following: render(:layout => false) </li></ul></ul><ul><ul><li>Whatever is in the corresponding .rhtml will replace the original contents of the <div> element </li></ul></ul>
  • 9. form_remote_tag() <ul><li>To use Ajax for any form, replace form_tag and end_form_tag with form_remote_tag and end_form_tag </li></ul><ul><li>All form elements will be serialized and sent to the server </li></ul><ul><li>They can be read from the params hash </li></ul>
  • 10. Observers <ul><li>Observers let you call Ajax actions when the user changed data on a form </li></ul><ul><li>Example (p. 392 in textbook): </li></ul><ul><ul><li><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> </li></ul></ul>
  • 11. CRUD <ul><li>Basic database operations are C reate, R ead, U pdate, and D elete </li></ul><ul><ul><li>The :scaffold method does a lot of the database work for you </li></ul></ul><ul><ul><li>Scaffolds are intended to be replaced </li></ul></ul><ul><li>ActiveRecord::Base provides all your basic needs </li></ul><ul><li>An ActiveRecord wraps a database; changes to the ActiveRecord result in changes to the database </li></ul><ul><ul><li>Each ActiveRecord object corresponds to a row in the database </li></ul></ul><ul><ul><li>The attributes of the object correspond to the column in the database </li></ul></ul>
  • 12. Some database methods <ul><li>new(attributes = nil) </li></ul><ul><ul><li>Creates a record with the given attributes </li></ul></ul><ul><li>find(*args) </li></ul><ul><ul><li>find by id: </li></ul></ul><ul><ul><ul><li>Person.find(1) # returns the object for ID = 1 </li></ul></ul></ul><ul><ul><li>find first: </li></ul></ul><ul><ul><ul><li>Person.find(:first) # returns the first object fetched by SELECT * FROM people </li></ul></ul></ul><ul><ul><li>find all: </li></ul></ul><ul><ul><ul><li>Person.find(:all) # returns an array of objects for all the rows # fetched by SELECT * FROM people </li></ul></ul></ul><ul><li>update(id, attributes) </li></ul><ul><ul><li>Creates a record with the given attributes </li></ul></ul><ul><li>destroy(id) </li></ul>
  • 13. References <ul><li>Agile Web Development with Rails , by Dave Thomas and David Hansson </li></ul><ul><ul><li>The Ajax material is from chapter 18, “The Web, V2.0” </li></ul></ul><ul><ul><li>Some of the forms information is from chapters 16 and 17 </li></ul></ul><ul><ul><li>Database material from chapter 14, especially 14.5 </li></ul></ul><ul><li>api.rubyonrails.com </li></ul><ul><ul><li>Database examples from ActiveRecord::Base </li></ul></ul><ul><ul><li>Some other examples from individual methods </li></ul></ul>
  • 14. The End

×