Upload
melvyn-reynolds
View
226
Download
0
Embed Size (px)
DESCRIPTION
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’.
Citation preview
The Controller
Carol WolfComputer Science
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
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’.
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.
The directory structure
The routes fileTestapp::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.
Listing courses – controller methodclass ScheduleController < ApplicationController
def indexend
def list_courses@courses = Course. find(:all, :order => "number")enddef find_courseend
end This will access the database table and return all
its contents, ordered by the number field.
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.
<h2>List all Courses</h2>
<h3><%= form_for :course, :url => {:action => :list_courses} do |form| %>
<p><%= submit_tag "List Courses" %></p><% end %></h3>
The controller responds by sending back list_courses.html.erb.
The list_courses.html.erb file<h1>Listing courses</h1> <table>
<tr><th>Number</th><th>Name</th>
<th>Credits</th> </tr>
<% @courses.each do |course| %> <tr> <td><%= course.number %></td> <td><%= course.name %></td> <td><%= course.credits %></td> </tr>
<% end %></table><br /><%= link_to 'Back', schedule_index_path %>
Index page code to find a single course<h2>Find a Course</h2><h3><%= form_for :course, :url => {:action
=> :find_course} do |form| %><p>
<label for="title">Course Number:</label><%= form.text_field :number, :size => 20 %>
</p><p><%= submit_tag "Find a Course" %></p>
<% end %></h3>
Index page
Controller code for finding a coursedef find_course
@params = params[:course]@course = Course.find_by_number(@params[:number])
respond_to do |format|
if @course != nilformat.html
elseformat.html { render :action => "not_found" }
endend
end
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.
Response page – find.course.html.erb<p id="notice"><%= notice %></p><p>
<b>Number:</b><%= @course.number %>
</p><p>
<b>Name:</b><%= @course.name %>
</p><p>
<b>Credits:</b><%= @course.credits %>
</p><%= link_to 'Back', schedule_index_path %>
Response page
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
<h3>The course was not found</h3>
<%= link_to 'Back', schedule_index_path %>