Download presentation
Presentation is loading. Please wait.
Published byMelvyn Reynolds Modified over 8 years ago
1
The Controller Carol Wolf Computer Science
2
Rails generate commands Using the generate command, you can create a number of useful objects. Rails: controller generator helper integration_test mailer migration model observer performance_test plugin resource scaffold scaffold_controller session_migration stylesheets
3
Generating a controller Use rails generate controller controller_name method1 method 2 … The controller’s name will be the one you give it followed by _controller. pizza_controller schedule_controller librarian_controller The new controller will have the methods named in the generate command. The views folder gets a new sub-folder with the name of the new controller. It will contain ERB files for each method. The file, route.rb, will get a new route for each one of the methods. They will all be ‘gets’. Some will have to be changed to ‘posts’.
4
A new controller for the schedule application The following will generate a new controller, views and routes: rails generate controller schedule index list_courses find_course The controller will be called schedule_controller. The methods are index, list_courses and find_course. The views are index.html.erb, list_courses.html.erb and find_course.html.erb. The new routes are get "schedule/index " get "schedule/list_courses " get "schedule/find_course" The first one can remain as a get, but the others must be post.
5
The directory structure
6
The routes file Testapp::Application.routes.draw do get "schedule/index" post "schedule/list_courses" post "schedule/find_course“ These changes are necessary in Rails. No parameters are sent when you access the index page. But they may be included with the other two. Whenever parameters may be used, Rails requires post.
7
Listing courses – controller method class ScheduleController < ApplicationController def index end def list_courses @courses = Course. find(:all, :order => "number") end def find_course end This will access the database table and return all its contents, ordered by the number field.
8
The index file The index file has a form that sends control to the list_courses method in the controller, which will respond to the request. List all Courses {:action => :list_courses} do |form| %> The controller responds by sending back list_courses.html.erb.
9
The list_courses.html.erb file Listing courses Number Name Credits
10
Index page code to find a single course Find a Course {:action => :find_course} do |form| %> Course Number: 20 %>
11
Index page
12
Controller code for finding a course def find_course @params = params[:course] @course = Course.find_by_number(@params[:number]) respond_to do |format| if @course != nil format.html else format.html { render :action => "not_found" } end
13
Controller code Parameters are sent to the controller from the form in a hash. Here the hash is “course” => {“number” => “CS 121”} We must first extract the hash. @params = params[:course] And then we can use the keys to extract the values. @params[:number] This is cumbersome, but it works.
14
Response page – find.course.html.erb Number: Name: Credits:
15
Response page
16
Not found page Often when searching the user makes a mistake. In that case the course will not be found. A simple page can be added to the views/schedule folder to catch these errors. not_found.html.erb The course was not found
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.