the controller carol wolf computer science. rails generate commands using the generate command, you...
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’.TRANSCRIPT
![Page 1: The Controller Carol Wolf Computer Science. Rails generate commands Using the generate command, you can create a number of useful objects. Rails:](https://reader033.vdocument.in/reader033/viewer/2022051203/5a4d1b847f8b9ab0599bc27e/html5/thumbnails/1.jpg)
The Controller
Carol WolfComputer Science
![Page 2: The Controller Carol Wolf Computer Science. Rails generate commands Using the generate command, you can create a number of useful objects. Rails:](https://reader033.vdocument.in/reader033/viewer/2022051203/5a4d1b847f8b9ab0599bc27e/html5/thumbnails/2.jpg)
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
![Page 3: The Controller Carol Wolf Computer Science. Rails generate commands Using the generate command, you can create a number of useful objects. Rails:](https://reader033.vdocument.in/reader033/viewer/2022051203/5a4d1b847f8b9ab0599bc27e/html5/thumbnails/3.jpg)
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’.
![Page 4: The Controller Carol Wolf Computer Science. Rails generate commands Using the generate command, you can create a number of useful objects. Rails:](https://reader033.vdocument.in/reader033/viewer/2022051203/5a4d1b847f8b9ab0599bc27e/html5/thumbnails/4.jpg)
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.
![Page 5: The Controller Carol Wolf Computer Science. Rails generate commands Using the generate command, you can create a number of useful objects. Rails:](https://reader033.vdocument.in/reader033/viewer/2022051203/5a4d1b847f8b9ab0599bc27e/html5/thumbnails/5.jpg)
The directory structure
![Page 6: The Controller Carol Wolf Computer Science. Rails generate commands Using the generate command, you can create a number of useful objects. Rails:](https://reader033.vdocument.in/reader033/viewer/2022051203/5a4d1b847f8b9ab0599bc27e/html5/thumbnails/6.jpg)
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.
![Page 7: The Controller Carol Wolf Computer Science. Rails generate commands Using the generate command, you can create a number of useful objects. Rails:](https://reader033.vdocument.in/reader033/viewer/2022051203/5a4d1b847f8b9ab0599bc27e/html5/thumbnails/7.jpg)
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.
![Page 8: The Controller Carol Wolf Computer Science. Rails generate commands Using the generate command, you can create a number of useful objects. Rails:](https://reader033.vdocument.in/reader033/viewer/2022051203/5a4d1b847f8b9ab0599bc27e/html5/thumbnails/8.jpg)
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.
![Page 9: The Controller Carol Wolf Computer Science. Rails generate commands Using the generate command, you can create a number of useful objects. Rails:](https://reader033.vdocument.in/reader033/viewer/2022051203/5a4d1b847f8b9ab0599bc27e/html5/thumbnails/9.jpg)
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 %>
![Page 10: The Controller Carol Wolf Computer Science. Rails generate commands Using the generate command, you can create a number of useful objects. Rails:](https://reader033.vdocument.in/reader033/viewer/2022051203/5a4d1b847f8b9ab0599bc27e/html5/thumbnails/10.jpg)
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>
![Page 11: The Controller Carol Wolf Computer Science. Rails generate commands Using the generate command, you can create a number of useful objects. Rails:](https://reader033.vdocument.in/reader033/viewer/2022051203/5a4d1b847f8b9ab0599bc27e/html5/thumbnails/11.jpg)
Index page
![Page 12: The Controller Carol Wolf Computer Science. Rails generate commands Using the generate command, you can create a number of useful objects. Rails:](https://reader033.vdocument.in/reader033/viewer/2022051203/5a4d1b847f8b9ab0599bc27e/html5/thumbnails/12.jpg)
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
![Page 13: The Controller Carol Wolf Computer Science. Rails generate commands Using the generate command, you can create a number of useful objects. Rails:](https://reader033.vdocument.in/reader033/viewer/2022051203/5a4d1b847f8b9ab0599bc27e/html5/thumbnails/13.jpg)
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.
![Page 14: The Controller Carol Wolf Computer Science. Rails generate commands Using the generate command, you can create a number of useful objects. Rails:](https://reader033.vdocument.in/reader033/viewer/2022051203/5a4d1b847f8b9ab0599bc27e/html5/thumbnails/14.jpg)
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 %>
![Page 15: The Controller Carol Wolf Computer Science. Rails generate commands Using the generate command, you can create a number of useful objects. Rails:](https://reader033.vdocument.in/reader033/viewer/2022051203/5a4d1b847f8b9ab0599bc27e/html5/thumbnails/15.jpg)
Response page
![Page 16: The Controller Carol Wolf Computer Science. Rails generate commands Using the generate command, you can create a number of useful objects. Rails:](https://reader033.vdocument.in/reader033/viewer/2022051203/5a4d1b847f8b9ab0599bc27e/html5/thumbnails/16.jpg)
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 %>