将包装器添加到所有设计视图

发布于 2025-01-02 05:07:31 字数 246 浏览 3 评论 0原文

我将 Devise 与 Rails 3.1 一起使用,并且我已经生成了 Devise 的所有视图。

我希望任何 Devise 视图都包含在 div 中:

<div class="devise-container">
  <!-- Devise view -->
</div>

目前我已手动将其添加到每个 Devise 视图中,但这不是很干燥。有更好的办法吗?

I'm using Devise with Rails 3.1 and I have generated all the views for Devise.

I want any Devise view to be wrapped in a div:

<div class="devise-container">
  <!-- Devise view -->
</div>

At the moment I have manually added this to every Devise view, but this is not very DRY. Is there a better way?

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

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

发布评论

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

评论(3

南薇 2025-01-09 05:07:31

假设您正在使用 Devise 控制器并且没有覆盖它们,您可以在常用的 app/views/layouts 中创建一个名为“devise.html.haml”或“devise.html.erb”(或您使用的任何模板语言)的布局/ 目录并添加您需要的自定义代码。

然后,该布局应自动加载并覆盖您的应用程序控制器,因为布局名称与控制器名称匹配。

然而,更好的解决方案可能是使用当前控制器名称 (controller.name) 将类添加到应用程序布局的标记中,并使用 CSS 通过添加自定义样式来管理问题仅限“body.devise”页面。该解决方案更加灵活,并且减少了应用程序代码的混乱,因为它是一个表示问题。

Assuming you are using the Devise controllers and have not overridden them, you can create a layout called "devise.html.haml" or "devise.html.erb" (or whatever templating language you use) in the usual app/views/layouts/ directory and add the custom code you need.

This layout should then automatically get loaded and override you application controller because the layout name matches the controller name.

However, that said, a better solution may be to add a class to the tag of your application layout with the current controller name (controller.name) and use CSS to manage the problem by adding a custom style only to "body.devise" pages. This solution is more flexible and adds less clutter to your application code, seeing as it is a presentation problem.

榕城若虚 2025-01-09 05:07:31

没有。或者您可以进行一些视图重构。

从设备安装视图与创建新视图文件完全相同。

在视图重构中,您可以做一些帮助器来完成这个特定的 div。或者做一些“布局”

There are not. Or you can do some view refactoring.

Install view from devise it's exactly the same than create new view file.

In View refactoring you can do some helper to do this particular div. Or do some "layout"

谈场末日恋爱 2025-01-09 05:07:31

我在视图/布局中使用

创建了一个 _devise-wrapper-start.html.erb ,并使用 _devise-wrapper-end.html.erb 创建了一个 _devise-wrapper-end.html.erb

然后在每个视图中我都放在

<%= render 'layouts/devise-wrapper-start' %>

开头和

<%= render 'layouts/devise-wrapper-end' %>

结尾。

这比仅仅将 HTML 放入设计视图中更加 DRY(不要重复)。

当我更改设计包装器时,它会在每个视图中发生变化,并且我不必手动修改每个视图。

这是一个半解决方案,但我没有找到更好的解决方案。

I created a _devise-wrapper-start.html.erb in views/layouts with

<div class="devise-container">

and a _devise-wrapper-end.html.erb with
</div>
and then in every view i put

<%= render 'layouts/devise-wrapper-start' %>

at the beginning and

<%= render 'layouts/devise-wrapper-end' %>

at the end.

This is more DRY (Don't Repeat Yourself) than just putting HTML inside devise views.

When i change the devise wrapper it changes in every view and i don't have to modify every view manually.

This is an half solution, but i didn't find any better.

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