rails 3,如何添加与应用程序其余部分不使用相同布局的视图? [英] rails 3, how add a view that does not use same layout as rest of app?

查看:15
本文介绍了rails 3,如何添加与应用程序其余部分不使用相同布局的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到关于如何构建我的应用程序以允许同一控制器中的不同视图使用完全不同的布局和样式表的任何文档或示例.

I could not find any docs or examples on how to structure my app to allow different views in the same controller to use completely different layouts and stylesheets.

我们的应用程序被搭建起来,然后我们使用漂亮的生成器来生成视图,然后添加用于身份验证的设计.我们有两种模型的视图和控制器:小部件和公司.

Our app was scaffolded and we then used nifty-generators to generate views, then added devise for authentication. We have views and controllers for two models: widgets and companies.

我目前只有一个布局:layouts/application.html.haml,我没有看到任何地方引用它,所以我假设(一个 rails 新手)它总是按命名约定使用.

I currently have a single layout: layouts/application.html.haml, I don't see that referenced anywhere so I assume (a rails newbie) that it's always used by naming convention.

我现在需要在相同的控制器中添加几个具有不同样式表和布局(例如,右上角没有登录/注销链接)的视图(用于移动浏览器).

I now need to add a couple of views (for mobile browsers) which have a different stylesheet and layout (for example, no login/logout links in the top right), within the same controllers.

这怎么办?

推荐答案

默认情况下,layouts/application.html.haml(.erb 如果你不使用 haml).

By default, layouts/application.html.haml (.erb if you are not using haml).

事实上,布局文件可以为每个控制器或每个动作设置,而不是每个视图、每个视图文件夹.

In fact, layout file could be set per controller or per action, instead of per view, per view folder.

有几种情况:

class ApplicationController < ActionController::Base
  layout "another"

  # another way
  layout :another_by_method
  private
  def another_by_method
    if current_user.nil?
      "normal_layout"
    else
      "member_layout"
    end
  end
end

将某个控制器中的所有动作更改为使用另一个布局文件

class SessionsController < ActionController::Base
  layout "sessions_layout"
  # similar to the case in application controller, you could assign a method instead
end

更改动作以使用其他布局文件

def my_action
  if current_user.nil?
    render :layout => "normal_layout"
  else
    render :action => "could_like_this", :layout => "member_layout"
  end
end

这篇关于rails 3,如何添加与应用程序其余部分不使用相同布局的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆