The Librarian Web Page Carol Wolf CS396X
Create new controller To create a new controller that can manage more than just books, type ruby script/generate controller librarian This creates librarian_controller.rb in the controllers folder librarian_helper.rb in the helpers folder Folder called views/librarian folder Controller file contains class LibrarianController < ApplicationController end The views folder is empty.
Views page we want to create
Index.html.erb The easiest way to create an erb file is to copy one generated by the scaffold command, rename it if needed and modify it. Here we can use index.html.erb in the views/books folder Listing books … Change the heading to Librarian and one by one, fill in the parts of the table.
CRUD – Create, Read, Update, Delete Create – add a new item to the database Read – list all items in the database or find a particular one. Update – change the data in a row or rows of the database. Delete – remove an item from the database (also called destroy.)
Erb code to list all books or find a book. List all books {:action => :list_books} do |form| %> Find a book {:action => :find_book} do |form| %> Title: 20 %>
class LibrarianController < ApplicationController def index end def = Book.find_books end def = = respond_to do |format| != nil format.html else flash[:notice] = 'Book was not found.' format.html { render :action => "index" } end
Create a new row in the database Ruby = Code for index page Create a book {:action => :new_book} do |form| %> Controller code is in new_book and create_book.
def new_book respond_to do |format| format.html # Send new_book.html.erb to the client. end def = Book.new(params[:book])# Get a new book with the parameters. respond_to do |format| Save the book in the database. flash[:notice] = 'The book was successfully created.' format.html { render :action => "show_book" } else flash[:notice] = 'Error: The book was not created.' format.html { render :action => "new_book" } end
Web page for new_book New book {:action => :create_book} do |f| %> ISBN: Author: Title: Image:
Creating the new row Client clicks on Create a book on the index page. The action in the form is new_book. The new_book method in the controller sends the web page new_book.html.erb to the client. It collects the information about the new book. The action in the form is create_book. This activates the create_book method in the controller. The controller creates a new book with the parameters = Book.new(params[:book]) If the book is saved in the database, the controller sends the web page, show_book.html.erb, back to the client.
Update a row of the database If the client clicks on update and supplies an id (isbn), the edit_book method in the controller is executed. def = = end This method finds the book in the database (if it’s there) and stores the data in the This data is used to populate a form, edit_book.html.erb, that is returned to the client. The client can then change the data in the form and submit the result to the controller.
Update_book page {:action => :update_book} do |f| %> ISBN: Author: Title: Image:
Hidden field The update_book.html.erb page has a hidden field. The controller will fill in this field with the id that was automatically generated when the table row was created, but it will not show the id to the client. This id can be used to save the modified row to the table rather than the isbn or some other field. The advantage to doing this is that all the displayed fields can be changed, since none of them are the primary key. A primary key cannot be altered.
Controller method, update_book def = params[:book] id the id from the = Book.find_by_id(id)#Find the book using the id. respond_to do |format| flash[:notice] = 'Book was successfully updated.' format.html { render :action => "show_book"} else flash[:notice] = 'Book was not updated.' end
Rails commands in controller method You can find a row in a database table using any of the fields. This one uses the id, which was in the hidden = Book.find_by_id(id) The rails command to update a row in the table This is much simpler to write than the SQL that rails translates it to.
Page returned to the client after changes Book Data ISBN: Author: Title: Image:
Deleting a row of the database table To delete a row, first get the data and display it for the client. Next ask the client to confirm that this is the row to be removed. If the client confirms that it should be deleted, remove it from the table. def = end If the client decides not to remove it, return to the index page.
Web page to delete a row of the table Delete Book {:action => :remove_book} do |f| %> ISBN: Author: Title: Image: