Create, Update and Delete
Carol WolfComputer 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.
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.
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.
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.
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>
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
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 %>
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.
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>
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
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>
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
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.
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>
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
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>
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>
Top Related