Rails redirect_to不同的控制器然后调用方法

时间:2015-03-28 21:14:57

标签: jquery ajax redirect ruby-on-rails-4 partials

我在Rails 4.1.8中工作,我的应用程序有一个管理员"仪表板"它使用自己的基本AJAX导航。我可以在方法之间导航到" manage_customers"," manage_accounts"和" manage_acct_transactions"正好。这个应用程序正在进行中。同样正常工作是在这些(AJAX渲染的部分)视图中执行一些基本的crud操作。

然而,

例如,"销毁"客户我重定向到管理员的主视图。仪表板,空白,因为" manage_foo"方法还没有被调用。

这是我在仪表板视图链接中调用的 Customers Controller 中的销毁方法

  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尝试了不同的语法,例如不同的路径,操作参数以及更改&#34; format.html&#34;到&#34; format.js&#34;

这是 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方法。就像你点击了&#34; manage_customers&#34; adminview中的链接。

这应该很容易。

1 个答案:

答案 0 :(得分:3)

您是否可以使用之前的操作在应用程序控制器中创建方法?

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]