【问题标题】:Rails redirect_to different controller and then call methodRails 重定向到不同的控制器,然后调用方法
【发布时间】: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


    【解决方案1】:

    你不能在你的应用程序控制器中创建一个带有前置操作的方法吗?

    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]
    

    【讨论】:

    • 感谢您的回答。我明白了它的作用,但是 before_action 需要有一些方法来有条件地运行该方法。我重定向到的视图只是一个外壳,它保存通过“管理”方法生成的内容。 IE。您重定向到客户、帐户或 acct_transactions 管理,无论您刚刚使用哪一个。另外,我很抱歉,但我并不清楚“manage_customers”方法不在客户控制器中,而是在一个名为管理员的单独控制器中(这些操作需要授权)更新了我的帖子以显示整个控制器。
    • 在您的应用程序控制器中,您可以尝试将 before 操作包装在条件 if 语句中,以检查控制器和/或正在执行的操作 - 例如 &lt;% if controller_name == "administrators" &amp;&amp; controller.action_name == "show"%&gt;
    • 将此标记为正确答案。但是,我已经取消了整个 ajax 导航协议。无论我怎么做,由于 Rails 的无状态特性,CSRF 错误都会出现问题。感谢您的帮助。
    • 没问题!很高兴能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 2018-10-22
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多