Ruby on Rails 中的 Asp.net MasterPage 等效项,尝试定义站点范围的布局
Asp.net WebForms 和 MVC 具有母版页的概念,可以轻松地为站点的所有页面定义一次性布局。在 Rails 中,我正在努力寻找等效的使用模式或功能。
根据我所读到的内容,在每个操作中定义布局非常容易:
layout: 'viewname'
现在,将其包含在每个控制器中似乎非常有仪式感,所以我添加了:
layout: 'application'
在基本 ApplicationController 中。
到目前为止,除非我在视图页面中有更具体的布局,否则一切正常。这是在 Rails 应用程序中获得一致风格的常用技术吗?
Asp.net WebForms and MVC has a concept of Masterpages which make it easy to define a one time layout for all the page of your site. In Rails I'm struggling to find an equivalent usage pattern or feature.
From what I've read it's really easy to define a layout in every action with:
layout: 'viewname'
Now that seemed pretty ceremonial to include in every controller so I added:
layout: 'application'
in the base ApplicationController.
So far this is working ok unless I have a more specific layout in the view pages. Is this common technique for getting a consistent style in your Rails application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
想象一个简化的博客,其中我们有一个名为 PostsController 的控制器,它有两个操作:index 和 show
当用户点击 时,将调用索引操作http://yourwebsite.com/posts - 此操作显示所有可用的博客文章。
当用户获取特定博客文章时,将调用显示操作 - 即 http://yourwebsite .com/posts/article-about-something-interesting
假设我们希望索引页面具有两列布局,并且希望每个博客文章的显示页面具有三列布局。为了实现这一点,我们只需定义两个单独的布局(在 app/views/layouts 文件夹中) - 我们将两列布局称为“应用程序”,将三列布局称为“备用”。
为了让索引页使用两列布局,显示页使用三列布局,我们可以在控制器中执行以下操作:
如果我们希望所有操作都使用相同的布局,我们可以这样做:
最后,如果我们没有指定要使用的布局,那么 Rails 将默认呈现与我们正在显示的资源同名的任何布局。因此,在我们的示例中,我们的资源称为“Posts”,如果我们定义了第三个名为 posts.html.erb 的布局(在 app/views/layouts 中),那么当用户呈现其中的任何操作时,Rails 将自动使用该布局。 PostsController - 当然前提是我们没有明确要求 Rails 渲染另一个布局......
希望它有帮助,
Imagine a simplified blog where we have a controller called PostsController which has two actions: index and show
The index action is called when the user hits http://yourwebsite.com/posts - this action displays all of the available blog posts.
The show action is called when a user gets a specific blog article - i.e. http://yourwebsite.com/posts/article-about-something-interesting
Let's say that we want the index page to have a two column layout and we want the show page for each blog-article to have a three column layout. To achieve this, we would simply define two separate layouts (in app/views/layouts folder) - we'll call the two column layout "application" and we'll call the three-column layout "alternate".
In order to get the index page to use the two-column layout and the show page to use the three-column layout, we could just do the following in our controller:
If we want all actions to use the same layout, we can just do this:
Finally, if we do not specify which layout we want to use, then Rails will by default, render any layout which has the same name as the resource we are displaying. So in our example, where our resources are called "Posts", if we define a third layout called posts.html.erb (in app/views/layouts) then Rails will automatically use that layout when the user renders any of the actions in the PostsController - providing of course that we have not explicitly asked Rails to render another layout....
Hope it helps,
这本 来自 Rails for .Net Developers 的 PDF 书籍摘录 对Rails 布局,以及与 ASP.Net MasterPages 的比较。由于它似乎工作得很好,因此可能会经常使用它,至少熟悉母版页概念的开发人员是如此。
This PDF book excerpt from Rails for .Net Developers has a pretty good explanation of Rails layouts, along with a comparison to ASP.Net MasterPages. Since it seems to work pretty well, it's probably used fairly often, at least by developers familiar with the master page concept.