Rails redirect_to 不同的控制器然后调用方法
Rails redirect_to different controller and then call method
我在 Rails 4.1.8 中工作,我的应用程序有一个管理员 "dashboard",它使用自己的基本 AJAX 导航。我可以在 "manage_customers"、"manage_accounts" 和 "manage_acct_transactions" 的方法之间导航就好了。这个应用程序正在进行中。在这些(AJAX 呈现部分)视图中执行一些基本的 crud 操作也很好。
然而,
例如,在 "Destroying" 一个客户之后,我重定向到管理员的主视图。仪表板,它是 空白 因为 "manage_foo" 方法尚未被调用。
这是我的 destroy 方法 在我的 Customers Controller 中,由仪表板视图调用 link:
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方法(如下)以显示分页客户列表,"refreshed" 当然没有删除的用户。
EDIT 下面是 整个 管理员控制器,为更好的视角添加。
我的 管理员控制器:
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 %>
我想做的就是在从客户控制器的销毁操作重定向到管理视图时调用管理员控制器的 manage_customers 方法。就像您在管理视图中单击 "manage_customers" link 时所看到的一样。
这应该很容易。
你能不能在你的应用程序控制器中创建一个方法,有一个之前的动作?
application_controller.rb
before_action :manage_customers
def manage_customers
@customers = Customer.order('lastname').page(params[:page]).per(20)
respond_to do |format|
format.js {render :manage_customers}
end
end
这样你可以在客户控制器之外的页面上调用它?不过,这将在您的应用程序中的任何其他操作之前调用,因此您可能必须将其设置得更具体,或者在子控制器上跳过它。
例如。在应用程序控制器中
before_filter :manage_customers, only: [:index, :show]
或在其他控制器中
skip_before_filter :manage_customers, except: [:index, :show]
我在 Rails 4.1.8 中工作,我的应用程序有一个管理员 "dashboard",它使用自己的基本 AJAX 导航。我可以在 "manage_customers"、"manage_accounts" 和 "manage_acct_transactions" 的方法之间导航就好了。这个应用程序正在进行中。在这些(AJAX 呈现部分)视图中执行一些基本的 crud 操作也很好。
然而,
例如,在 "Destroying" 一个客户之后,我重定向到管理员的主视图。仪表板,它是 空白 因为 "manage_foo" 方法尚未被调用。
这是我的 destroy 方法 在我的 Customers Controller 中,由仪表板视图调用 link:
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方法(如下)以显示分页客户列表,"refreshed" 当然没有删除的用户。
EDIT 下面是 整个 管理员控制器,为更好的视角添加。 我的 管理员控制器:
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 %>
我想做的就是在从客户控制器的销毁操作重定向到管理视图时调用管理员控制器的 manage_customers 方法。就像您在管理视图中单击 "manage_customers" link 时所看到的一样。
这应该很容易。
你能不能在你的应用程序控制器中创建一个方法,有一个之前的动作?
application_controller.rb
before_action :manage_customers
def manage_customers
@customers = Customer.order('lastname').page(params[:page]).per(20)
respond_to do |format|
format.js {render :manage_customers}
end
end
这样你可以在客户控制器之外的页面上调用它?不过,这将在您的应用程序中的任何其他操作之前调用,因此您可能必须将其设置得更具体,或者在子控制器上跳过它。
例如。在应用程序控制器中
before_filter :manage_customers, only: [:index, :show]
或在其他控制器中
skip_before_filter :manage_customers, except: [:index, :show]