加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – M Hartl的Ruby on Rails教程第5章主页上的自定

发布时间:2020-12-16 19:34:51 所属栏目:百科 来源:网络整理
导读:在Michael Hartl的RoR教程中:第5章末尾的练习涉及简化RSpec测试. 5.37(http://ruby.railstutorial.org/chapters/filling-in-the-layout#sec-layout_exercises)定义了主页的几个测试. 在文件spec / helpers / application_helper_spec.rb中: require 'spec_
在Michael Hartl的RoR教程中:第5章末尾的练习涉及简化RSpec测试.

5.37(http://ruby.railstutorial.org/chapters/filling-in-the-layout#sec-layout_exercises)定义了主页的几个测试.
在文件spec / helpers / application_helper_spec.rb中:

require 'spec_helper'

describe ApplicationHelper do

  describe "full_title" do
  .
  .
  .
  .
    it "should not include a bar for the home page" do
      full_title("").should_not =~ /|/
    end
  end
end

要通过此测试,我需要显示主页标题:
“Ruby on Rails教程示例应用程序”
而不是“Ruby on Rails Tutorial Sample App | Home”

但是,本教程并未指导我如何编写更改代码.

这是我尝试过的:

在app / views / static_pages / home.html.erb中:

>删除:

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

在app / views / layouts / application.html.erb中
将代码编辑为:

<title>
 <% if :title
  full_title = "Ruby on Rails Tutorial Sample App | " && yield(:title)
 else
  full_title = "Ruby on Rails Tutorial Sample App"
 end %>
 <%= print full_title %>
</title>

我的noob大脑的其他变化可以集合.
这是我想要完成的事情:

如果页面没有提供标题,请返回“Ruby on Rails Tutorial Sample App”

如果提供了标题,则返回“Ruby on Rails Tutorial Sample App | #PageTitle”

任何建议表示赞赏.

解决方法

那这个呢?
module ApplicationHelper

  # Returns the full title on a per-page basis.
  def full_title(page_title)
    base_title = "Ruby on Rails Tutorial Sample App"
    if page_title.empty?
      base_title
    else
      "#{base_title} | #{page_title}"
    end
  end
end

http://ruby.railstutorial.org/chapters/rails-flavored-ruby#code-title_helper

只需在视图中调用方法即可.所以它应该是这样的< title><%= full_title(yield(:title))%>< / title>正如他所说.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读