Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如果用户登录,则设置不同的Rails模板_Ruby On Rails_Templates_Layout - Fatal编程技术网

Ruby on rails 如果用户登录,则设置不同的Rails模板

Ruby on rails 如果用户登录,则设置不同的Rails模板,ruby-on-rails,templates,layout,Ruby On Rails,Templates,Layout,我试图在用户登录时将布局设置为私有。如果我能做以下事情,那就太完美了: layout 'private' if current_user 除此之外,它给出了以下错误: undefined local variable or method `current_user' for ApplicationController:Class 我还尝试在应用程序控制器中执行此操作: before_filter :pick_the_layout def pick_the_layout if curre

我试图在用户登录时将布局设置为私有。如果我能做以下事情,那就太完美了:

layout 'private' if current_user
除此之外,它给出了以下错误:

undefined local variable or method `current_user' for ApplicationController:Class
我还尝试在应用程序控制器中执行此操作:

before_filter :pick_the_layout

def pick_the_layout
  if current_user
    render :layout => "private"
  else
    render :layout => "public"
  end
end
这个也不走运,这是有道理的。有人能想出更好的方法吗?

嗯。。。大概
如果定义了布局“专用”?当前用户?

您可以这样做:

layout :determine_layout

private
  def determine_layout
    current_user ? "private" : "public"
  end
显然,这取决于当前_用户是否为nil或false(如果用户未登录)

您可以在此处查看有关布局用法的文档:

这里有更多的例子:


令人惊叹的这工作非常完美,我喜欢它的干净。我从没想过要把布局设置成那样。非常感谢你的帮助和链接。好吧,这没有给出任何错误,但也没有呈现任何内容。不过谢谢你。