Download presentation
Presentation is loading. Please wait.
Published byBeverly Arnold Modified over 9 years ago
1
Forms Carol Wolf Computer Science
2
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
3
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.
4
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| %> …
5
The index page
6
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 data: @params = params[:pizza] @crust = @params[:crust] The output code on the response page: The crust is.
7
Select Drop-down Boxes The index page code: : "small", :Large => "large"} %> The controller code: @size = @params[:size] 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.
8
Check Boxes – Index page code Check boxes allow the user to make several choices. :
9
Check Boxes – Controller code @toppings = Array.new if @params[:mushrooms] == "1" @toppings << "Mushrooms" end if @params[:pepperoni] == "1" @toppings << "Pepperoni" end if @params[:vegetables] == "1" @toppings << "Vegetables" end
10
Check Boxes – Response page code The toppings you chose are:
11
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 code: @delivery_year = @params["delivery_date(1i)"] @delivery_month = @params["delivery_date(2i)"] @delivery_day = @params["delivery_date(3i)"] @delivery_hour = @params["delivery_time(4i)"] @delivery_minute = @params["delivery_time(5i)"] Response page code: The delivery date is / /, and the delivery time is :.
12
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 code: @address = @params[:address] The response page code: The address is.
13
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 code: @telephone = @params[:telephone] The response page code: The telephone number is. Link to return to index page:
14
The Response Page – order.html.erb
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.