…"> …">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

15-Jun-15 Rails and Ajax. HTML Forms The... tag encloses form elements (and usually includes other HTML as well) The arguments to form tell what to do.

Similar presentations


Presentation on theme: "15-Jun-15 Rails and Ajax. HTML Forms The... tag encloses form elements (and usually includes other HTML as well) The arguments to form tell what to do."— Presentation transcript:

1 15-Jun-15 Rails and Ajax

2 HTML Forms The... 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"(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 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: :save }, { :class=>"compact" } %> …

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 => "store", :action => "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: 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 The basic RHTML link is " method_name ") %> where method_name uses the url_for() format The basic Ajax/RHTML link is id_of_element_to_update, :url => { :action => : method_name } ) %> which refers to Original text

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 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): Search term: 0.5, :update => :results, :url => { :action => :search }) %>

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 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 api.rubyonrails.com Database examples from ActiveRecord::Base Some other examples from individual methods

14 The End


Download ppt "15-Jun-15 Rails and Ajax. HTML Forms The... tag encloses form elements (and usually includes other HTML as well) The arguments to form tell what to do."

Similar presentations


Ads by Google