Search code examples
ruby-on-rails-4will-paginate

Returning view to page I started from when deleting a user in a Rails view


Environment: OS: Windows 8.1 Ruby: 2.1.5 Rails: 4.2

In my views/users/index.html.erb, I have:

<h1>Listing Users</h1>

<span class="eastdev_pagination">

<%= will_paginate @users, :container => false %>
</span>

<table class="table table-striped">
  <thead>
  <tr>
    <th>First</th>
    <th>Last</th>
    <th>Work phone</th>
    <th>City</th>
    <th>Organization</th>
    <th colspan="3"></th>
  </tr>
  </thead>

  <tbody>
  <% @users.each do |u| %>
    <tr>
      <td><%= u.first %></td>
      <td><%= u.last %></td>
      <% if u.work_phone %>
        <% phone = number_to_phone(u.work_phone, area_code: true) %>
        <td><%= phone %></td>
      <% else %>
        <td></td>
      <% end %>
      <td><%= u.city %></td>
      <td><%= u.organization %></td>
      <%= render partial: "layouts/show_edit_del_buttons", locals: {my_model: u} %>
    </tr>
  <% end %>
  </tbody>
</table>
<span class="eastdev_pagination">
  <%= will_paginate @users, :container => false %>
</span>

<br>

In my layouts/show_edit_del_buttons.html.erb, I have:

<td>
  <%= link_to my_model, class: 'btn btn-info', title: 'View', 'data-toggle' => 'tooltip', 'data-placement' => 'right' do %>
    <span class="glyphicon glyphicon-search"></span>
  <% end %>
<td>
<% if staff_access_level? %>
  <td>
    <%= link_to [:edit, my_model], class: 'btn btn-warning', title: 'Edit', 'data-toggle' => 'tooltip', 'data-placement' => 'right' do %>
      <span class="glyphicon glyphicon-pencil"></span>
    <% end %>
  </td>
  <td>
    <%= link_to my_model, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger', title: 'Delete', 'data-toggle' => 'tooltip', 'data-placement' => 'right'  do %>
      <span class="glyphicon glyphicon-remove"></span>
    <% end %>
  </td>
<% end %>   

In my controllers/users.rb, I have the following:

def index
    if params && params.has_key?('search-form')
      item = params['search-form']
      @users = User.where("first = ? OR last = ? OR organization LIKE ? AND approved = 1", item, item, "%#{item}%").paginate(:page => params[:page], per_page: 5).order('last')
    else
      @users = User.where("approved = 1").paginate(page: params[:page], per_page: 5)
    end
end

def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to users_url }
      format.json { head :no_content }
    end
end 

The issue I am having, is that when I delete a user, I am returned to the first page. For example, I am on page 5 of the pagination, and I delete a user here, I am returned to the first navigation page.

How do I get the view to go back to the page I was on prior to the deletion?

Notes:

If I try something like:

<%= link_to my_model(:page => params[:page]), method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger', title: 'Delete', 'data-toggle' => 'tooltip', 'data-placement' => 'right'  do %>
    <span class="glyphicon glyphicon-remove"></span>
<% end %>>

I get an error message:

Method not found: my_model      

Solution

  • You should redirect user after delete on page you were on. Look at your destroy method

    redirect_to users_url

    Here you should define a page while redirect. Like this

    my_model = Your path, like user_path(user, page: params[:page])

    <%= link_to user_path(user, page: params[:page]), method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger', title: 'Delete', 'data-toggle' => 'tooltip', 'data-placement' => 'right'  do %>
      <span class="glyphicon glyphicon-remove"></span>
    <% end %>
    

    You will get current page number in side destroy method

    def destroy
       @user.destroy
       respond_to do |format|
          format.html { redirect_to params[:page] ? users_url(page: params[:page]) : users_url }
          format.json { head :no_content }
       end
    end