【发布时间】:2015-06-02 01:56:54
【问题描述】:
我在 Rails 4.1.8 中工作,我的应用程序有一个管理“仪表板”,它使用自己的基本 AJAX 导航。我可以在“manage_customers”、“manage_accounts”和“manage_acct_transactions”的方法之间导航。这个应用程序正在进行中。在这些(AJAX 渲染的部分)视图中执行一些基本的 crud 操作也可以正常工作。
然而,
例如,在“破坏”客户之后,我重定向到管理员的主视图。仪表板,它是空白,因为尚未调用“manage_foo”方法。
这是我的 Customers Controller 中的 destroy 方法,由仪表板视图链接调用:
def destroy
@customer.destroy
respond_to do |format|
format.html { redirect_to adminview_administrator_path, notice: 'Customer was successfully destroyed.' }
format.json { head :no_content }
end
end
它把我带到了正确的地方,但没有内容。 我如何执行此重定向并然后调用 manage_customers 方法(如下)以显示分页客户列表,当然“刷新”没有删除的用户。
编辑下面是整个管理员控制器,为了更好的视角而添加。 我的管理员控制器:
class AdministratorsController < ApplicationController
before_filter :authenticate_user!
before_action :set_administrator, only: [:show, :edit, :update, :destroy]
before_action :require_admin
# GET /administrators
# GET /administrators.json
def index
@administrators = Administrator.all
end
def adminview
end
# Update adminview content using AJAX and jQuery
def manage_accounts
@accounts = Account.order('id').page(params[:page]).per(20)
respond_to do |format|
format.html
format.js {render :manage_accounts}
end
end
def manage_customers
@customers = Customer.order('lastname').page(params[:page]).per(20)
respond_to do |format|
format.js {render :manage_customers}
end
end
def manage_acct_transactions
@acct_transactions = AcctTransaction.order('date').page(params[:page]).per(20)
respond_to do |format|
format.html
format.js {render :manage_acct_transactions}
end
end
# GET /administrators/1
# GET /administrators/1.json
def show
end
# GET /administrators/new
def new
@administrator = Administrator.new
end
# GET /administrators/1/edit
def edit
end
# POST /administrators
# POST /administrators.json
def create
@administrator = Administrator.new(administrator_params)
respond_to do |format|
if @administrator.save
format.html { redirect_to @administrator, notice: 'Administrator was successfully created.' }
format.json { render :show, status: :created, location: @administrator }
else
format.html { render :new }
format.json { render json: @administrator.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /administrators/1
# PATCH/PUT /administrators/1.json
def update
respond_to do |format|
if @administrator.update(administrator_params)
format.html { redirect_to @administrator, notice: 'Administrator was successfully updated.' }
format.json { render :show, status: :ok, location: @administrator }
else
format.html { render :edit }
format.json { render json: @administrator.errors, status: :unprocessable_entity }
end
end
end
# DELETE /administrators/1
# DELETE /administrators/1.json
def destroy
@administrator.destroy
respond_to do |format|
format.html { redirect_to administrators_url, notice: 'Administrator was successfully destroyed.' }
format.json { head :no_content }
end
end
def require_admin
unless current_user.role == 'admin'
redirect_to root_path, alert: 'NOT AUTHORIZED!! Redirecting to home page..'
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_administrator
@administrator = Administrator.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def administrator_params
params[:administrator]
end
end
我在destroy方法中为format.html尝试了不同的语法,例如不同的路径,一个动作参数,以及将“format.html”改为“format.js”
这里是 routes.rb(管理员控制器部分):
resources :administrators do
member do
get :adminview
get :manage_accounts
get :manage_customers
get :manage_acct_transactions
end
end
需要做的是重定向到 adminview.html.erb,它有一个 div,我在其中生成 manage_customers 函数和相关视图的输出。如下:
manage_customers.js.erb..
$('#displayArea').html('<%= escape_javascript(render partial: 'manage_customers') %>');
_manage_customers.html.erb(这是部分)..
<h3>Listing All Customers</h3>
<%= paginate (@customers) %>
<table id="indexTable" class="table table-striped">
<thead>
<tr>
<th>Customer ID</th>
<th>Customer Name</th>
<th>Termination</th>
</tr>
</thead>
<tbody>
<% @customers.each do |customer| %>
<tr>
<td><%= '%09d' % customer.id %></td>
<td><%= customer.lastname %>, <%= customer.firstname %></td>
<td><%= link_to 'Destroy', customer, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Account', new_customer_path %>
我要做的就是在从客户控制器的销毁操作重定向到 adminview 时调用管理员控制器的 manage_customers 方法。就像您在管理视图中单击“manage_customers”链接所看到的一样。
这应该很容易。
【问题讨论】:
标签: jquery ajax redirect ruby-on-rails-4 partials