(a) <%= form_tag 'show' do %> (b) <%= text_field_tag :celsius, @c %> (c) <%= submit_tag 'Convert Temperature' %> (d) <% end %>Ans: Line a says that the data values in any control in the form (between lines a and d) will be submitted back to the controller method corrsponding to the action convert. Line b defines the text field control as having the name :celsius and initial value @c. Line c says that the caption on the submit button is Convert Temperature. The form is submitted to the controller when the submit button is clicked. Line b terminates the form. A different IRB delimiter is used for the end statement because end is a control statement and does not generate any HTML code on the client end.
(a) input = params[:cel] (b) @c = input.to_i (c) @f = 9 * @c / 5 + 32Ans: (a) params[:cel] obtains the string from the :cel control and assigns it to the local variale input. (b) Assigns input converted to an integer to the instance variable @c. (c) Converts @c to Fahrenheit and assigns it to the instance variable @f.
post "/controller/action"
Control | Tag Name | type attribute |
---|---|---|
Text Field | input | text |
Submit Button | input | submit |
# Example 1 a = [3, 4, 5, 6, 7] a.each do |i| puts i + 1 end Output: 4 5 6 7 8 # Example 2 sum = 0 r = [3, 4, 5, 6, 7] r.each do |i| sum = sum + i end puts sum Output: 25 (The sum of the numbers in the array.) # Example 3 pets = ['fido', 'felix', 'jerry'] pets.each do |p| puts "I love my pet " + p + "." end Output: I love my pet fido. I love my pet felix. I love my pet jerry.
rake db:seedNow start the Rails console and load all of the records from the database into an array with
a = Student.allUse for..each statements or each methods to print the first and last names for all the grade records:
a.each do |g| print "#{g.first_name} #{g.last_name}\n" end
# Assign the array of table rows # to the instance variable @a. @a = Person.all
body { background-color: rgb(245,245,255) }
body { background-color: #f5f5ff; }
# 1. Create a range that includes the end using new method. r = Range.new(1, 5) # 2. Shortcut to create a range that includes the end. r = 1..5
<% form_tag 'show' do %> ... Controls go here. <% end %>
<% form_for @item, :url => { :action => 'show' } do |f| %> ... Controls go here. <% end %>
<%= text_field_tag :celsius, '20', :class => 'tf' %>:celsius is the name; '20' is the initial value.
<%= f.text_field :celsius, :class => 'tf' %>:celsius is the field of the database object f.
<%= select_tag :source_rate, options_for_select([['Dollars', 1.00000], ['Euro', 1.36749], ['Pesos', 12.6149], ['Pounds', 1.58228]]), :class => 'ddm' %>:source_rate is the name; the first column of the array are the values displayed in the dropdown menu; the second column are the values returned to the controller to be read by a params statement. the option tags are the HTML option tags.
<%= f.select :source_rate, [['Dollars', 1.00000], ['Euro', 1.36749], ['Pesos', 12.6149], ['Pounds', 1.58228]], :class => 'ddm' %>:source_rate is the field of the database object f; the values of the dropdown menu are specified in the array as they were in the FormTagHelper version; note that for the FormHelper version, no options_for_select method is needed.
<%= radio_button_tag :group, value, true %>:group sets the button group (in a group of radio buttons, only can be checked at a time)
<%= f.radio_button :group, value, :checked => true %>:group sets the button group (in a group of radio buttons, only can be checked at a time)
<%= check_box_tag :sugar, 1, true, :class => 'cb' %>:sugar is the name of the checkbox
<%= f.check_box :sugar, 1, :checked => true, :class => 'cb' %>:sugar is the name of the checkbox