Ruby on Rails - 渲染布局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1128080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:27:50  来源:igfitidea点击:

Ruby on Rails - render layout

ruby-on-railslayoutrender

提问by RyanJM

I'm trying to split up a web site into two sections. One which should use the application layout and one that should use the admin layout. In my application.rb I created a function as follows:

我试图将一个网站分成两个部分。一种应该使用应用程序布局,一种应该使用管理布局。在我的 application.rb 中,我创建了一个函数,如下所示:

def admin_layout
  if current_user.is_able_to('siteadmin')
    render :layout => 'admin'
  else
    render :layout => 'application'
  end
end

And in the controllers where it might be one or the other I put

在控制器中,它可能是我放置的一个或另一个

before_filter :admin_layout

This works fine for some pages (where its just text) but for others I get the classic error:

这适用于某些页面(其中只是文本),但对于其他页面,我得到了经典错误:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

Does anyone have an idea of what I'm missing? How should I properly use render and layout?

有没有人知道我错过了什么?我应该如何正确使用渲染和布局?

回答by molf

The method renderwill actually attempt to render content; you should not call it when all you want to do is set the layout.

该方法render实际上将尝试呈现内容;当您只想设置布局时,您不应该调用它。

Rails has a pattern for all of this baked in. Simply pass a symbol to layoutand the method with that name will be called in order to determine the current layout:

Rails 有一个适用于所有这些的模式。只需将一个符号传递给layout具有该名称的方法将被调用以确定当前布局:

class MyController < ApplicationController
  layout :admin_layout

  private

  def admin_layout
    # Check if logged in, because current_user could be nil.
    if logged_in? and current_user.is_able_to('siteadmin')
      "admin"
    else
      "application"
    end
  end
end

See details here.

在此处查看详细信息

回答by RyanJM

perhaps you need to check that the user is signed in first?

也许您需要先检查用户是否已登录?

def admin_layout
  if current_user and current_user.is_able_to 'siteadmin'
    render :layout => 'admin'
  else
    render :layout => 'application'
  end
end

回答by marcgg

It might be because current_useris nilwhen the user is not logged in. Either test for .nil?or initialize the object.

这可能是因为current_usernil当在不登录的用户。无论是测试.nil?或初始化对象。

回答by Reuben Mallaby

Try molf's answer with:

试试摩尔夫的回答:

if logged_in? and current_user.is_able_to('siteadmin')

如果登录?和 current_user.is_able_to('siteadmin')

回答by Dee-M

Your current user is properbly set in the after the user has logged in. and in this case you should have an option to determine if you are logged in

您的当前用户在用户登录后正确设置。在这种情况下,您应该有一个选项来确定您是否已登录

like

喜欢

 if !@current_user.nil?
   if @current_user.is_able_to("###")
     render :layout => "admin"
   else
    render :layout => "application"
   end
 end

Then it will only enter the if statement if your @current_user in not nil.

那么它只会在你的@current_user 不为零时才进入 if 语句。