Ruby-on-rails rails 3,如何添加与应用程序其余部分不使用相同布局的视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5090084/
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
rails 3, how add a view that does not use same layout as rest of app?
提问by jpw
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.
我们的应用程序被搭建起来,然后我们使用漂亮的生成器来生成视图,然后添加用于身份验证的设计。我们有两种模型的视图和控制器:小部件和公司。
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.
我目前只有一个布局:layouts/application.html.haml,我没有看到任何地方引用它,所以我假设(一个 rails 新手)它总是被命名约定使用。
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.
我现在需要在相同的控制器中添加几个视图(对于移动浏览器),它们具有不同的样式表和布局(例如,右上角没有登录/注销链接)。
How can that be done?
那怎么办呢?
回答by PeterWong
By default, layouts/application.html.haml(.erbif you are not using haml).
默认情况下,layouts/application.html.haml(.erb如果您不使用 haml)。
In fact, layout file could be set per controller or per action, instead of per view, per view folder.
事实上,布局文件可以按控制器或按动作设置,而不是按视图、按视图文件夹设置。
There are few cases:
有几种情况:
To change the default layout file for all controller (ie. use another.html.hamlinstead of application.html.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
To change all actions in a certain controller to use another layout file
更改某个控制器中的所有操作以使用另一个布局文件
class SessionsController < ActionController::Base
layout "sessions_layout"
# similar to the case in application controller, you could assign a method instead
end
To change an action to use other layout file
更改操作以使用其他布局文件
def my_action
if current_user.nil?
render :layout => "normal_layout"
else
render :action => "could_like_this", :layout => "member_layout"
end
end
回答by Dty
Yes, you can use different layouts and stylesheets within the same controllers.
是的,您可以在同一个控制器中使用不同的布局和样式表。
The rails guideon layouts is a good place to start. Look at Section 3 - Structuring Layouts
布局的导轨指南是一个很好的起点。查看第 3 部分 - 构建布局
There are several ways to use a different layout but one of the easiest is to simply add a file with the same name as your controller in the layouts/folder. So if your controller is PostsControllerthen adding a layouts/post.html.hamlwould cause rails to use that layout. If no such layout is found, and no other layouts are specified, rails will use the default of layouts/application.html.haml
有多种方法可以使用不同的布局,但最简单的一种方法是在layouts/文件夹中添加一个与控制器同名的文件。因此,如果您的控制器PostsController添加了 alayouts/post.html.haml会导致 rails 使用该布局。如果没有找到这样的布局,并且没有指定其他布局,rails 将使用默认的layouts/application.html.haml
回答by Lesly Revenge
If you do not want to go too complex, you can simply do this:
如果你不想太复杂,你可以简单地这样做:
layout 'layout_one'
def new
@user= User.new
render layout: 'landing_page'
end
this will do.
这会做。
回答by zechtz
I am sure there's plenty of answers to this but here's another one where you can use different layouts per controllers or per action.
我相信对此有很多答案,但这里有另一个答案,您可以在其中为每个控制器或每个动作使用不同的布局。
class ListingsController < ApplicationController
# every action will use the layout template app/views/layouts/listing_single.html.erb
layout 'listing_single'
# the 'list' action will use the layout set in the 'alternative_layout' method
# you can also add multiple actions to use a different layout,just do like layout :alternative_layout, only: [:list,:another_action]
layout :alternative_layout, :only => :list
def show
end
def list
end
private
def alternative_layout
if current_user.is_super_admin?
#if current use is super admin then use the layout found in app/views/layouts/admin.html.erb otherwise use the layout template in app/views/layouts/listing_list.html.erb
'admin'
else
'listing_list'
end
end
end
回答by dannymcc
Well, if it's a different view for mobile devices but all desktop versions are the same then you could use JQtouch.
好吧,如果它是移动设备的不同视图,但所有桌面版本都相同,那么您可以使用 JQtouch。
http://railscasts.com/episodes/199-mobile-devices
http://railscasts.com/episodes/199-mobile-devices
# config/initializers/mime_types.rb
Mime::Type.register_alias "text/html", :mobile
# application_controller.rb
before_filter :prepare_for_mobile
private
def mobile_device?
if session[:mobile_param]
session[:mobile_param] == "1"
else
request.user_agent =~ /Mobile|webOS/
end
end
helper_method :mobile_device?
def prepare_for_mobile
session[:mobile_param] = params[:mobile] if params[:mobile]
request.format = :mobile if mobile_device?
end
The above code is taken from the Railscasts example.
上面的代码取自 Railscasts 示例。

