jpw*_*ynn 51 layout ruby-on-rails
我找不到任何关于如何构建我的应用程序的文档或示例,以允许同一控制器中的不同视图使用完全不同的布局和样式表.
我们的应用程序是脚手架,然后我们使用漂亮的生成器生成视图,然后添加设计用于身份验证.我们有两个模型的视图和控制器:小部件和公司.
我目前只有一个布局:layouts/application.html.haml,我没有看到任何地方引用,所以我假设(一个新手),它总是被命名约定使用.
我现在需要在相同的控制器中添加一些视图(对于移动浏览器),这些视图具有不同的样式表和布局(例如,右上角没有登录/注销链接).
怎么办?
Pet*_*ong 129
默认情况下,layouts/application.html.haml(.erb如果你没有使用haml).
实际上,可以为每个控制器或每个操作设置布局文件,而不是每个视图文件夹的每个视图.
几个案例:
another.html.haml而不是application.html.haml)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
Run Code Online (Sandbox Code Playgroud)
class SessionsController < ActionController::Base
layout "sessions_layout"
# similar to the case in application controller, you could assign a method instead
end
Run Code Online (Sandbox Code Playgroud)
def my_action
if current_user.nil?
render :layout => "normal_layout"
else
render :action => "could_like_this", :layout => "member_layout"
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37570 次 |
| 最近记录: |