Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 The Librarian Web Page Carol Wolf CS396X

2 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.

3 Views page we want to create

4 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.

5 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.)

6 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 %>

7 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

8 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.

9 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

10 Web page for new_book New book {:action => :create_book} do |f| %> ISBN: Author: Title: Image:

11 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.

12 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.

13 Update_book page {:action => :update_book} do |f| %> ISBN: Author: Title: Image:

14 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.

15 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

16 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.

17 Page returned to the client after changes Book Data ISBN: Author: Title: Image:

18 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.

19 Web page to delete a row of the table Delete Book {:action => :remove_book} do |f| %> ISBN: Author: Title: Image:


Download ppt "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."

Similar presentations


Ads by Google