首页 > 解决方案 > Passing Local Variable in Rails 5 Best Practice

问题描述

I am trying to dry up my code and decided to use a partial for my page title and pass a local variable into it. It works, but I want to know if I am conforming to the Rails best practice.

partials/_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 I would add this to the top of all my views

<% 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>

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

解决方案


推荐阅读