ruby-on-rails - 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>
解决方案
推荐阅读
- javascript - 在 ASP.NET Core MVC 应用程序中使用 JavaScript 切换按钮
- react-native - 在 react native 和 firebase firestore 中实现重复日期
- python - 无法导入名称“CatBoostRegressor”
- cmd - 根据 git 分支构建 jenkins
- android - 从移动应用客户端触发 Firestore 功能的问题
- python - Python比较两个字符串,并检查特定的子字符串值
- node.js - 在 Bitbucket 管道中部署 Firebase Cloud 功能时出错
- c# - 异步方法未从任务列表中正确报告
- c# - P/Invoke 和 .NET 目标框架
- azure - 通过 terraform 部署时 LUN 编号未排序