create, update and delete carol wolf computer science

18
Create, Update and Delete Carol Wolf Computer Science

Upload: alyson-erica-foster

Post on 17-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Create, Update and Delete Carol Wolf Computer Science

Create, Update and Delete

Carol WolfComputer Science

Page 2: Create, Update and Delete Carol Wolf Computer Science

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.

Page 3: Create, Update and Delete Carol Wolf Computer Science

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.

Page 4: Create, Update and Delete Carol Wolf Computer Science

index.html.erb<h2>Add a Course</h2>

<h3><%= form_for :course, :url => {:action => :new_course} do |form| %>

<p>

<%= form.submit "Add Course" %>

</p>

<% end %>

</h3>

Because there is a button, the method is ‘post’. The action gives the name of the controller method, here new_course.

Page 5: Create, Update and Delete Carol Wolf Computer Science

The controller method new_coursedef 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.

Page 6: Create, Update and Delete Carol Wolf Computer Science

new_course.html.erb

<h1>New course</h1>

 

<%= form_for :course, :url => {:action => :create_course} do |f| %>

<p><b>Number:</b><br />

<%= f.text_field :number %></p>

<p><b>Name:</b><br />

<%= f.text_field :name %></p>

<p><b>Credits:</b><br />

<%= f.text_field :credits %></p>

<p><%= f.submit "Add Course" %></p>

<% end %>

 

<h3><%= link_to 'Back', schedule_index_path %></h3> 

Page 7: Create, Update and Delete Carol Wolf Computer Science

The controller method create_coursedef 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

end

end

Page 8: Create, Update and Delete Carol Wolf Computer Science

show_course.html.erb<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 9: Create, Update and Delete Carol Wolf Computer Science

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.

<p> <%= f.hidden_field :id %></p>

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.

Page 10: Create, Update and Delete Carol Wolf Computer Science

The index file<h2>Update a Course</h2>

<h3><%= form_for :course, :url => {:action => :edit_course} do |form| %>

<p>

<label for="number">Course Number:</label>

<%= form.text_field :number, :size => 10 %>

</p>

<p>

<%= form.submit "Update Course" %>

</p>

<% end %>

</h3>

 

Page 11: Create, Update and Delete Carol Wolf Computer Science

The controller - updatedef 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

end

Page 12: Create, Update and Delete Carol Wolf Computer Science

edit_course.html.erb

<h1>Editing course</h1>

 

<%= form_for :course, :url => {:action => :update_course} do |f| %>

<p><b>Number:</b><br />

<%= f.text_field :number %></p>

<p><b>Name:</b><br />

<%= f.text_field :name %></p>

<p><b>Credits:</b><br />

<%= f.text_field :credits %></p>

<p><%= f.hidden_field :id %></p>

<p><%= f.submit "Update" %></p>

<% end %>

 

<h3><%= link_to 'Back', schedule_index_path %></h3>

 

Page 13: Create, Update and Delete Carol Wolf Computer Science

The controller – update_coursedef 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

end

end

 

Page 14: Create, Update and Delete Carol Wolf Computer Science

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.

Page 15: Create, Update and Delete Carol Wolf Computer Science

Delete – index.html.erb<h2>Delete a course</h2>

<h3><% form_for :course:url => {:action => :delete_course} do |form| %>

<p>

<label for="CRN">CRN:</label>

<%= form.text_field :number, :size => 10 %>

</p>

<p><%= submit_tag "Delete Course" %></p>

<% end %></h3>

Page 16: Create, Update and Delete Carol Wolf Computer Science

The controller – delete_coursedef 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

end

Page 17: Create, Update and Delete Carol Wolf Computer Science

delete_course.html.erb<h2>Delete Course</h2>

 

<%= form_for :course, :url => {:action => :remove_course} do |f| %>

<p><b>Number:</b><br />

<%= f.text_field :number %></p>

<p><b>Name:</b><br />

<%= f.text_field :name %></p>

<p><b>Credits:</b><br />

<%= f.text_field :credits %></p>

<p><%= f.submit "Delete Course? Yes, remove the course?" %></p>

<% end %>

 

<h3><%= link_to 'No, do not remove.', schedule_index_path %></h3>

Page 18: Create, Update and Delete Carol Wolf Computer Science

The controller - remove_coursedef remove_course

@params = params[:course]

@course = Course.find_by_number(@params[:number])

@course.destroy

End

Response – remove_course.html.erb<h2>The course was removed.</h2>

 

<h3><%= link_to 'Back', schedule_index_path %></h3>