Rails:嵌套视图或局部视图或..? [英] Rails: Nested views or partials or..?

查看:104
本文介绍了Rails:嵌套视图或局部视图或..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Ruby on Rails的新手,我来自Drupal/PHP背景.

I new to Ruby on Rails, and I am coming from Drupal/PHP background.

我正在尝试做一些简单的事情.用于某些页面(例如关于我们,免责声明等)的静态"控制器和用于视频的视频"控制器.

I am trying to do something simple. A "Static" controller for some pages like about us, disclaimer etc. and a "Video" controller for videos.

整个应用程序具有相同的外观.我试图弄清楚如何避免重复视图.

The whole application has same look. I am trying to figure out how I can avoid duplicating views.

在drupal中,主题如下所示: html.tpl.php(提供page.tpl.php) -page.tpl.php(提供多个block.tpl.php) -block.tpl.php(提供更多嵌套的tpls或由主题函数生成的html).

In drupal the theming goes like this: html.tpl.php (renders page.tpl.php) - page.tpl.php (renders multiple block.tpl.php) - block.tpl.php (renders more nested tpls or some html generated by theme functions).

我觉得这很棒.

在Rails中,我读到的是application.html.erb(我认为它类似于Drupal的html.tpl.php,因为它通常包含整个应用程序中可用的样式表,js等),称为布局".然后是视图",在此布局中屈服"了内容.我们在控制器中指定视图,否则它会自动获取一个.我认为一个视图为Drupal的"page.tpl.php".

In rails, what I read is there is application.html.erb (Which I think is similar to html.tpl.php of Drupal, as it normally contains stylesheets, js, etc. available throughout the application) called "Layout". Then there are "Views", which "yield" content in this layout. We specify the view in controller, or it gets one automatically. I think of a view as "page.tpl.php" of Drupal.

我的大多数网站都使用相同的结构,即页眉,顶部菜单,内容,右侧边栏和页脚.但是,各区域的内容根据路径而变化.所以我正在考虑这样做:

Most of my site uses same structure i.e. header, top menu, content, right sidebar, footer. But content of each area changes depending on the path. So I am thinking of doing it like this:

  • 静态"和"视频"将调用同一视图,但具有不同的变量值,即内容区域和侧边栏的值.
  • 此视图将使用'render'渲染另一个视图,并从控制器传递变量的子集,即它将渲染内容&侧边栏视图.
  • 如果需要,这些视图将呈现更多视图,依此类推.

我读到的另一篇是偏文.我似乎无法区分在另一个视图内部渲染视图和局部视图.

Another one I read about is partials. I can't seem to differentiate between rendering a view inside another view, and rendering a partial.

哪种方法更好?还是有更好的铁路方式"?

Which method is better? Or is there a better "Rails Way"?

推荐答案

这是一个相当大的主题,需要介绍,但编辑的重点是

This is quite a large topic to cover but edited highlights are

如果您想在每个页面上重复内容,例如标题或导航栏,则将其放入layouts/application.html.erb

If you want to repeat content on every page, like a header or navbar for example then put it in layouts/application.html.erb

如果您要使页面的内容保持整洁的视图,则可以使用局部视图.通常,在视图中,我创建一个名为shared的文件夹,然后将我的局部文件放在其中.例如,如果我保存了一个名为_form.html.erb的局部文件(请确保您在文件名的开头使用下划线命名该局部文件),那么我会在我的视图中将其命名为

if you want to keep your views tidy with content specific to that page then you can use partials. Normally in views I create a folder called shared and then put my partials in there. for example if i save a partial called _form.html.erb (make sure you name partials with the underscore at the beginning of the file name), i would then call that in my view like

<%= render 'shared/form' %>

您还可以在布局/应用程序文件中使用局部变量以保持整洁

you can also use partials in the layouts/application file to keep that clean aswell

这只是我的操作方式,我相信经验更多的Rails家伙会提供更好的方法

This is just my way of doing it, I'm sure more experienced rails guys have better ways

希望有帮助,然后再问其他问题

Hope that helps, any more questions then just ask

这篇关于Rails:嵌套视图或局部视图或..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆