在Rails中传递局部变量5最佳实践

时间:2018-06-11 19:57:30

标签: ruby-on-rails ruby-on-rails-5

我正在尝试干掉我的代码并决定使用部分页面标题并将局部变量传递给它。它有效,但我想知道我是否符合Rails的最佳实践。

分音/ _page-header.html.erb

<section class="hero is-primary">
  <div class="hero-body">
    <div class="container">
      <h1 class="title">
        <%= title %>
      </h1>
      <h2 class="subtitle">
        Hero subtitle
      </h2>
    </div>
  </div>
</section>

static_pages / home.html.erb 我会将此添加到我的所有观点的顶部

<% provide(:title, 'Home') %>
...

application.html.rb

<!doctype html>
<html lang="en">
<head>
  <%= render 'partials/head' %>
  <%= render 'partials/head-scripts' %>
  <%= render 'layouts/shim' %>
</head>
<body>
<%= render 'partials/nav' %>
<%= render 'partials/page-header', title: yield(:title) %>
<section class="section">
  <div class="container">
    <div class="content">
      <%= yield %>
    </div>
  </div>
</section>
<%= render 'partials/footer' %>
<%= render 'partials/footer-scripts' %>
</body>
</html>

0 个答案:

没有答案