【问题标题】:specified layout is not rendered for a controller if its action is called from other controller如果从其他控制器调用其操作,则不会为控制器呈现指定的布局
【发布时间】:2015-07-12 16:48:02
【问题描述】:

在我的 rails 应用程序中,我有一个控制器“home”和另一个“admin”。从家庭控制器的索引动作我试图呈现控制器的索引动作。在管理控制器中,我指定了要使用的布局,即“user_layout”,并且主控制器使用默认应用程序布局。 以下是家庭控制器

class HomeController < ApplicationController

  def index
    if user_signed_in?

        @user = User.where(id: current_user.id)
      @user.each do |user|
        @allowed_user = user.allow
      end

        @org = Organization.where(user_id: current_user.id)
        #render :template => 'organizations/show'
      @org.each do |org|
        session[:current_organization_id] = org.id
      end
        respond_to do |format|
            format.html { render 'admin/index' }
            format.json { render json:@org}
        end 
    end
  end
end

管理员控制器

class AdminController < ApplicationController

    before_filter :authenticate_user!
    layout "user_layout"
  def index
    @org = Organization.where(user_id: current_user.id)
    @user = User.where(id: current_user.id)
      @user.each do |user|
        @allowed_user = user.allow
      end
  end
end

但是,如果在访问其他控制器的其他操作后调用管理控制器的索引操作,则应用 user_layout 但从主控制器调用时不会应用。

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    渲染视图不去控制器,所以它不会去管理控制器并检查它应该渲染哪个布局。您必须在家庭控制器中指定它。

    请试试这个。

      format.html { render "admin/index", layout: "user_layout"  }
    

    在您对索引操作的响应块中,主控制器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-30
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      • 2013-12-13
      • 1970-01-01
      相关资源
      最近更新 更多