Forms Carol Wolf Computer Science
The Controller To create a controller, type rails generate controller pizza index order This creates a controller called pizza_controller.rb. Two routes in routes.rb: “get pizza/index” and “get pizza/order”. The second one will have to be changed to a post. “post pizza/order” Two ERB files in views/pizza index.html.erb order.html.erb
Example: Pizzeria An order form for pizza will be used as an example to show different kinds of buttons. radio buttons select drop-down boxes check boxes date and time boxes text areas text fields Each will have a place in the form on the index page, code in the controller to access the data and an output section on the response page, order.html.erb.
The Basic Form The basic form has a name and action. The method is determined by the corresponding route in routes.rb. It has a number of places for inputting data and a submit button. The following comes from a pizzeria example. {:action => :order} do |form| %> …
The index page
Radio Buttons Radio buttons are used when only one answer may be given. The erb code in the index file: : The controller code that receives the = The output code on the response page: The crust is.
Select Drop-down Boxes The index page code: : "small", :Large => "large"} %> The controller The response page code: The size is. The name of the parameter is size. The values that will be stored in the drop down box are listed in a hash. The one selected will be sent in the parameters hash.
Check Boxes – Index page code Check boxes allow the user to make several choices. :
Check Boxes – Controller = Array.new == << "Mushrooms" end == << "Pepperoni" end == << "Vegetables" end
Check Boxes – Response page code The toppings you chose are:
Date and Time Boxes The date and time boxes provide drop-down boxes with all the months, about 10 years, 31 days, 24 hours and 60 minutes. Index file code: : Controller = = = = Response page code: The delivery date is / /, and the delivery time is :.
Text areas Text areas are used to collect a section of text. They are sent to the controller as a string with lines separated by “\r\n”. The index code: 'float: left' %> 3, :cols => 40 %> The controller The response page code: The address is.
Text Fields Text fields are probably used the most. The consist of a single box where users can type in data. The index page code: The controller The response page code: The telephone number is. Link to return to index page:
The Response Page – order.html.erb