Presentation is loading. Please wait.

Presentation is loading. Please wait.

Create, Update and Delete Carol Wolf Computer Science.

Similar presentations


Presentation on theme: "Create, Update and Delete Carol Wolf Computer Science."— Presentation transcript:

1 Create, Update and Delete Carol Wolf Computer Science

2 Basic operations on databases  Create – Add a new table to the database  Read – List all objects in a table or find a specific one  Update – Change one or more fields in an object  Delete – Remove an object from the table  Requests and responses are the usual ERB files.  Controller code is somewhat complicated, particularly for update.  You can generate new controller methods and ERB files using rails generate controller … -s.  The –s tells Rails to skip existing methods.

3 Create  To add a create method to the controller, type rails generate controller schedule new_course show_course –s  This does not get a new controller. Instead it adds two new methods as well as generating new views.  Create requires two methods, one to show a form that will collect the data for the new object and a second to add the result to the database table.  It also adds two new routes to routes.rb. They must both be changed to posts.  The code for the index file only has to send the user to the server.

4 index.html.erb Add a Course {:action => :new_course} do |form| %>  Because there is a button, the method is ‘post’. The action gives the name of the controller method, here new_course.

5 The controller method new_course def new_course respond_to do |format| format.html # new_course.html.erb end End  This just sends a view to the user with text fields for entering the data.  The view, new_course.html.erb, contains an action that sends the data to the controller method, create_course.

6 new_course.html.erb New course {:action => :create_course} do |f| %> Number: Name: Credits:

7 The controller method create_course def create_course @course = Course.new(params[:course]) respond_to do |format| if @course.save flash[:notice] = 'The course was successfully created.' format.html { render :action => "show_course" } else flash[:notice] = 'Error: The course was not created.' format.html { render :action => "new_course" } end

8 show_course.html.erb Number: Name: Credits:

9 Update  The only tricky part in update is using the id that is automatically generated by Rails to keep track of the object.  Using it allows us to make changes to all the fields.  Since it is not of interest to the user, it is stored in a hidden field.  But since it is one of the parameters, it can be used to store the record back in the database.  We begin, as before, by generating new methods for the controller and views.

10 The index file Update a Course {:action => :edit_course} do |form| %> Course Number: 10 %>

11 The controller - update def edit_course @params = params[:course] @course = Course.find_by_number(@params[:number]) if @course == nil flash[:notice] = 'The course was not found.' redirect_to(:action => "not_found" ) end

12 edit_course.html.erb Editing course {:action => :update_course} do |f| %> Number: Name: Credits:

13 The controller – update_course def update_course @params = params[:course] id = @params[:id] @course = Course.find_by_id(id) respond_to do |format| if @course.update_attributes(params[:course]) flash[:notice] = 'Course was successfully updated.' format.html { render :action => "show_course"} else flash[:notice] = 'Course was not updated.' format.html { render :action => "edit_course" } end

14 Delete  When deleting an object, the user should be given a chance to view it and decide if it should go.  This can be done using confirm. This was done in the index file generated by the scaffold command.  Another way is just to give the user a choice of deleting the object or returning to the mail page without making a change.

15 Delete – index.html.erb Delete a course {:action => :delete_course} do |form| %> CRN: 10 %>

16 The controller – delete_course def delete_course @params = params[:course] @course = Course.find_by_number(@params[:number]) if @course != nil respond_to do |format| format.html end else flash[:notice] = 'The course was not found.' end

17 delete_course.html.erb Delete Course {:action => :remove_course} do |f| %> Number: Name: Credits:

18 The controller - remove_course def remove_course @params = params[:course] @course = Course.find_by_number(@params[:number]) @course.destroy End  Response – remove_course.html.erb The course was removed.


Download ppt "Create, Update and Delete Carol Wolf Computer Science."

Similar presentations


Ads by Google