Rails Redirect fails after delete. (Delete is successful but display of remaining records does not happen)

973 views Asked by At

When a user calls the users_path view, all users are rendered correctly in the display.

However I also redirect to index after a delete. The console indicates a 200 Ok but then the index is not rendered.

Trying to step through some of the rails code, it looks like in the case of after the delete, rails does an implicit render. It doesn't do this in the first case of calling the index view.

views/users/index.rb

<% @users.each do |user| %>
<tr class = '<%=cycle('dataeven', 'dataodd')%>' > 
<td class = '<%=cell_class%>'><%= user.username %></td>
<td <class = '<%=cell_class%>'><%= user.email %></td>
<td class = '<%=cell_class%>'><%= user.actual_name %></td>
<td class = '<%=link_cell_class%>'><%= tlink_to 'edit_permissions', permissions_path(user) %></td>
<td class = '<%=link_cell_class%>'><%= tlink_to 'reset_password', edit_password_path(user) %></td>
<td class = '<%=link_cell_class%>'><%= unlock_link(user) %></td>
<td class = '<%=link_cell_class%>'><%= tlink_to("destroy", destroy_user_path(user), {:navigate=>false, :method=>'delete', :remote=>true, :data=>{:confirm=>tmessage( 'delete.are_you_sure', $W, {:model=>user.username}) }})%></td>
</tr>
<%end%>
</table>
<%= twill_paginate %>
<br>

<%= link_to t('headings.new.heading', :model=> t($ARM + 'user', :count=>1)), new_user_path %> | <%= tlink_to "new_invitation", new_user_invitation_path%></li>

users_controller.rb

def index
  @users = User.paginate :page => params[:page], :per_page => 15
  respond_to do |format|
    format.html 
    format.xml  { render :xml => @translation_languages }
  end
end

..... users_controller.rb ...

def destroy    
  @user.destroy
  tflash('delete', :success, {:model=>@@model, :count=>1})
  respond_to do |format|
    format.html { redirect_to(users_url) }
    format.xml  { head :ok }
  end
end

Has any one any idea what may cause the redirect to silent stop

3

There are 3 answers

2
Mark Swardstrom On

I see the link is remote - this will be a js format.

Do this:

Create a destroy.js.erb file and put it in the same directory with index. You can do javascript in there, for example - remove the element

in views/users/destroy.js.erb

$("##{dom_id(user)}").remove()

In the view, add this to the user row:

<tr class = '<%=cycle('dataeven', 'dataodd')%> <%= dom_id(user) %>' > 

EDIT

You can also just remove the remote: true. Should have mentioned that.

1
Chris Fritz On

Two questions which may help lead you to a solution:

  1. What are the contents of the tflash method? (Does it work if you comment it out?)
  2. Why does your destroy action respond to xml?
1
dukha On

Thanks for the suggestions guys.

The solution turned out to be painfully obvious: All the other deletes in this application had involved a full refresh of the index page after the delete. For whatever reason, I put a remote: true in this one and then didn't put in any js to deal with the deleted row in html.

So now in users_controller.rb

 def destroy
  @user.destroy
  tflash('delete', :success, {:model=>@@model, :count=>1, :now=> true})
  respond_to do |format|
    format.html { redirect_to(users_url) }
    format.js {}
  end
end

and in users/index.html.erb

<tr class = '<%=cycle('dataeven', 'dataodd')%>' id = '<%= dom_id(user)%>' >     
  <td class = '<%=link_cell_class%>'><%= tlink_to("destroy", destroy_user_path(user), {:navigate=>false, :method=>'delete', :remote=>true, :data=>{:confirm=>tmessage( 'delete.are_you_sure', $W, {:model=>user.username}) }})%></td>

and in views/users/destroy.js.erb

$('#user_<%[email protected] %>').remove();

I'll change all my deletes to use ajax in this way.