O'Reilly logo

Ajax on Rails by Scott Raymond

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Forms

So far we’ve been using helpers to generate links that request information from the server, but for really interesting applications, we’ll want to send data to the server as well, and that means forms. First, we’ll create a simple, non-Ajax form. The form_tag and end_form_tag helpers create an HTML form element. For example, this:

<%= form_tag :action => 'reverse' %>
<%= end_form_tag %>

...generates this:

<form action="/chapter3/reverse" method="post">
</form>

Form Tag Helpers

Within a form, there are helpers to generate input fields. Here they are:

text_field_tag( name , value = nil , options = {} )

The keys in the options hash will be made into HTML attributes. For example:

<%= text_field_tag "name", "Scott",
     :size     => 5,
     :disabled => true,
     :style    => "background-color: red" %>

The helper will produce this output:

<input type="text" name="name" id="name" value="Scott" 
  size="5"
  disabled="disabled"
  style="background-color: red" />
hidden_field_tag( name , value = nil , options = {} )

Takes the same options as text_field_tag.

password_field_tag( name = "password" , value = nil , options = {} )

Takes the same options as text_field_tag.

file_field_tag( name , options = {} )

Takes the same options as text_field_tag.

check_box_tag( name , value = "1" , checked = false , options = {} )

Takes the same options as text_field_tag.

radio_button_tag( name , value , checked = false , options = {} )

Takes the same options as text_field_tag.

text_area_tag( name , content = nil , options = {} )

Takes ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required