rails 3,如何添加一个视图,该视图不使用与其他应用程序相同的布局? [英] rails 3, how add a view that does not use same layout as rest of app?
问题描述
我找不到任何有关如何构建我的应用程序的文档或示例,以允许同一控制器中的不同视图使用完全不同的布局和样式表.
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
(如果您不使用haml,则为.erb
).
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屋!