将视图返回到我在 Rails 视图中删除用户时开始的页面

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

环境: OS: Windows 8.1 Ruby:2.1.5 Rails: 4.2

在我的 views/users/index.html.erb 中,我有:

<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>

在我的 layouts/show_edit_del_buttons.html.erb 中,我有:

<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 %>   

在我的 controllers/users.rb 中,我有以下内容:

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 

我遇到的问题是,当我删除用户时,我返回到第一页。比如我在第5页的分页,我在这里删除了一个用户,我又回到了第一个导航页。

如何让视图返回到删除之前所在的页面?

备注:

如果我尝试这样的事情:

<%= 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 %>>

我收到一条错误消息:

Method not found: my_model      

您应该在删除您所在的页面后重定向用户。 看看你的销毁方法

redirect_to users_url

这里你应该在重定向时定义一个页面。 像这样

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 %>

您将在 side destroy 方法中获取当前页码

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