Reputation: 11
I am having trouble implementing the DELETE action on any object that I have for my application.
To show you the general code for my delete action in the Group Controller:
def destroy
@group = Group.find(params[:id])
respond_to do |format|
if @group.delete
format.html { redirect_to group_path }
format.json { head :no_content }
else
format.html { redirect_to group_path }
format.json { head :no_content }
end
end
end
And the code for my View layout:
<h1>My Groups</h1>
<table>
<tr>
<th>Name</th>
<th>Users</th>
<th></th>
<th></th>
</tr>
<% @groups.each do |group| %>
<tr>
<td><%= group.name %></td>
<td>
<% group.memberships.each do |membership| %>
<%= User.find(membership.user_id).name %>
<br/>
<% end %>
</td>
<td><%= link_to 'Show', group %></td>
<td><%= link_to 'Destroy', group, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Group', new_group_path %>
I am unable to delete the group object even though the rails server log gives me back a 200 OK response. The returned page is a blank screen:
Started DELETE "/groups/2" for 127.0.0.1 at 2013-03-19 01:22:01 +0800
Processing by GroupsController#destroy as HTML
Parameters: {"authenticity_token"=>"aH+z0DlL7NeoVlxda8Td76WdnrH7/G8UyWDqbJcTu9w=", "id"=>"2"}
Completed 200 OK in 0ms (ActiveRecord: 0.0ms)
ActiveRecord wasn't used and there were no changes to the database. There were no such problems in the past, but I only realized that I could not delete any kind of object recently.
I have been trying to find solutions to similar problems found on the net, but unfortunately it seems like no one has had this problem yet.
Update 1:
The following is my application layout:
<!DOCTYPE html>
<html>
<head>
<%= csrf_meta_tag %>
<title>Home</title>
<%= javascript_include_tag :application %>
<%= stylesheet_link_tag :application, :media => "all" %>
<%= javascript_include_tag "/javascripts/main.js" %>
<%= stylesheet_link_tag "/css/main.css" %>
</head>
<body>
<%= yield %>
</body>
</html>
But when I use the console to delete the object. It works:
1.9.3-p194 :003 > Group.find(23).delete
Group Load (0.5ms) SELECT "groups".* FROM "groups" WHERE "groups"."id" = $1 LIMIT 1 [["id", 23]]
SQL (3.0ms) DELETE FROM "groups" WHERE "groups"."id" = 23
=> #<Group id: 23, name: "groupie", created_at: "2013-03-18 15:29:42", updated_at: "2013-03-18 15:29:42", user_id: nil>
Upvotes: 0
Views: 222
Reputation: 11
I just found the solution to the question. I actually wrote a code for an around_filter that messed up the destroy session for every controller:
around_filter :clear_registration_id_on_destroy_session, only: :destroy
def clear_registration_id_on_destroy_session
is_filter_active = (controller_path == 'devise/sessions' && params[:action] == 'destroy')
if is_filter_active && user_signed_in?
if current_user.update_attribute(:device_platform, nil)
logger.info "Updated device platform attributes to nil"
end
if current_user.update_attribute(:registration_id, nil)
logger.info "Updated registration id attributes to nil"
end
yield
end
end
The problem is that I didn't yield anything for any other controller other than devise:sessions#destroy.
So for those who are as forgetful as me, please remember to think about the other conditions when is_filter_active is not true.
def clear_registration_id_on_destroy_session
is_filter_active = (controller_path == 'devise/sessions' && params[:action] == 'destroy')
if is_filter_active && user_signed_in?
if current_user.update_attribute(:device_platform, nil)
logger.info "Updated device platform attributes to nil"
end
if current_user.update_attribute(:registration_id, nil)
logger.info "Updated registration id attributes to nil"
end
yield
else
yield
end
end
Upvotes: 1
Reputation: 26528
It sounds like your delete link is just acting as a link to the show
action, as the method
option is not being picked up.
You need to have the appropriate javascript included in your application layout, as the delete link requires JS to use the correct HTTP method.
If you are using Rails >= 3.1 make sure you have the following in the header of the layout file you are using for rendering the page:
<%= javascript_include_tag :application %>
Also make sure you have the csrf_meta_tag in your layout:
<%= csrf_meta_tag %>
Upvotes: 0