Download presentation
Presentation is loading. Please wait.
Published byMax Doar Modified over 10 years ago
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 list_books @books = Book.find_books end def find_book @params = params[:book] @book = Book.find_a_book(@params[:title]) respond_to do |format| if @book != 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 commands @book = Book.new(params[:book]) @book.save 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 create_book @book = Book.new(params[:book])# Get a new book with the parameters. respond_to do |format| if @book.save# 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 sent, @book = 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 edit_book @params = params[:book] @book = Book.find_by_isbn(@params[:isbn]) end This method finds the book in the database (if it’s there) and stores the data in the variable @book. 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 update_book @params = params[:book] id = @params[:id]#Extract the id from the parameters. @book = Book.find_by_id(id)#Find the book using the id. respond_to do |format| if @book.update_attributes(params[:book]) 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 field. @book = Book.find_by_id(id) The rails command to update a row in the table is @book.update_attributes(params[:book]) 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 remove_book @params = params[:book] @book = Book.find_by_isbn(@params[:isbn]) @book.destroy 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:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.