简体   繁体   English

如何仅显示Controller X的特定布局

[英]How do I only display a specific layout for Controller X

Let's say I only want to display a specific layout when I'm on the home page. 假设我只想在主页上显示特定的布局。 The home page's controller and action is home#index . 主页的控制器和操作是home#index

How do I display a different layout for business#new ? 如何为business#new显示不同的布局?

There are couple of options: 有两种选择:

Option 1 - Specific Layout at Controller level 选项1-控制器级别的特定布局

Just create a layout file named business.html.erb in app/views/layouts and it will be automatically picked for rendering ALL the views of BusinessController 只需在app/views/layouts创建一个名为business.html.erb的布局文件,它将被自动选中以呈现BusinessController所有视图

Option 2 - Custom layout name at Controller level 选项2-控制器级别的自定义布局名称

Create a layout file named my_custom_name.html.erb in app/views/layouts and in your controller specify it as: app/views/layouts创建一个名为my_custom_name.html.erb的布局文件,并在您的控制器中将其指定为:

class BusinessController < ApplicationController
  layout "my_custom_name"
  #...
end

my_custom_name layout would be used for ALL the views corresponding to BusinessController . my_custom_name布局将用于与BusinessController对应的所有视图。

Option 3 - Layout for a particular action in a Controller 选项3-控制器中特定动作的布局

Create a layout file named layout_name.html.erb in app/views/layouts and in your controller specify it as: app/views/layouts创建一个名为layout_name.html.erb的布局文件,并在您的控制器中将其指定为:

class BusinessController < ApplicationController

   def new
     # ...
     render :layout => "layout_name"
   end 
end

In this case, layout_name layout would only be applied for rendering new.html.erb page. 在这种情况下, layout_name布局将仅应用于呈现new.html.erb页面。

you can specify the layout of a controller with 您可以使用以下命令指定控制器的布局

class businessController < ApplicationController
  layout "business_layout"
  #...
end

http://guides.rubyonrails.org/layouts_and_rendering.html 2.2.14 Finding Layouts http://guides.rubyonrails.org/layouts_and_rendering.html 2.2.14查找布局

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用link_to链接到索引控制器,但仅列出具有特定属性的那些对象? - How do i use link_to in order to link to an index controller but listing only those objects with a specific attribute? 如何在主布局中使用控制器方法? - How do I use a controller method in the main layout? 如何在Rails的单独查看页面中仅显示“特定”项目? - How do I display only “Specific” project(s) on a separate View Page in Rails? 如何为特定的:动作使用不同的布局? - How do I use a different Layout for a specific :action? 如果控制器的值为nil,如何显示0? - if a value from controller is nil, how do I display 0? 如何使用angularjs从控制器显示验证错误? - How do i display validation error from controller using angularjs? 如何在Rails 3的另一个控制器中重定向到特定操作? - How do I redirect to a specific action in a different controller in Rails 3? 在Rails中,如何验证特定控制器动作的模型? - In Rails, how do I validate a model for a specific controller action? 如何在Rails 3.2.1中使用特定于控制器的样式表? - How do I use Controller specific stylesheets in Rails 3.2.1? 如何在rails 3.1中显示控制器特定的javascript? - How to display controller specific javascript in rails 3.1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM