我应该将“global”放在哪个文件夹中共享部分模板?

发布于 2024-11-18 05:40:01 字数 178 浏览 2 评论 0原文

我正在使用 Ruby on Rails 3.0.7,并且计划使用部分模板。我的应用程序中的所有类都将使用相同的部分,因此我必须决定将所有这些放在哪里。

将“全局”共享部分模板放在 lib 文件夹中是个好主意吗?如果不是,选择放置这些文件夹的常见做法是什么?关于如何正确命名和加载该文件夹有什么建议吗?

I am using Ruby on Rails 3.0.7 and I am planning to use partial templates. All classes in my application would use same partials so I have to decide where to located all those.

Is it a good idea to put "global" shared partial templates in the lib folder? If no, what is a common practice to choose the folder where to put those? Any advice on how to properly name and load that folder?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖树树初阳… 2024-11-25 05:40:01

标准是将所有共享部分放在 app/views/shared 中,并将它们引用为

render :partial => 'shared/partial_name'

如果您有一个标准的“列表中的行”部分(例如,对于索引页),则可以使用共享部分如:

# To render a single object row:
render :partial => 'shared/item', :locals => { :item => @item }
# Or to render them all:
render :partial => 'shared/item', :collection => @items

The standard is placing all shared partials in app/views/shared, and referencing them as

render :partial => 'shared/partial_name'

If you have a standard "row in a list" partial (say, for an index page), you could use a shared partial like:

# To render a single object row:
render :partial => 'shared/item', :locals => { :item => @item }
# Or to render them all:
render :partial => 'shared/item', :collection => @items
﹏雨一样淡蓝的深情 2024-11-25 05:40:01

Rails 4:

将您打算在整个应用程序中使用的部分放在/app/views/application中,

然后您可以轻松地在应用程序中的任何位置:

render partial: 'partial_name', variable_name: variable

额外的好处是您始终可以通过在 /app/views/controller_name/_partial_name.html.erb 中重新定义该部分的含义来覆盖特定视图空间中的部分,然后对部分的调用将引用您更具体的上下文进来了如果不存在,您将获得应用程序级别的部分。

建议取自 Thoughtbot

Rails 4:

put the partials you intend to use through out your application in /app/views/application

Then anywhere in your application you can easily:

render partial: 'partial_name', variable_name: variable

The added benefit is that you can always override the partial in a particular view space by redefining what that partial means in /app/views/controller_name/_partial_name.html.erb and the calls to the partial will then reference the more specific context you're in. If that doesn't exist you get the application level partial.

Suggestion taken from Thoughtbot

不再见 2024-11-25 05:40:01

约定是将它们放在 app/views/shared

如果您将有许多部分,我建议您将它们放入该文件夹的子目录中,无论对您的应用程序有意义,因为一个目录中有很多部分文件通常不是一种好的做法。

Conventions is to put them under app/views/shared

If you're going to have many partials, I'd recommend you putting them into subdirectories of that folder, whatever makes sense to your application, since having many partials in one directory is generally not a good practice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文