Railsチュートリアル4.1.2(https://railstutorial.jp/chapters/rails_flavored_ruby?version=5.1#cha-rails_flavored_ruby)にて学習中です。
rails testをかけたところ、以下のエラーが表示されます。
<Ruby on Rails Tutorial Sample App> expected but was
<SampleApp>..
Expected 0 to be >= 1.
test/controllers/static_pages_controller_test.rb:18:in `block in class:StaticPagesControllerTest'
おそらくapplication.html.erbが適用されておらず、Sample Appの記載があるhome.html.erbが直接表示されている為では無いかと思っております。
application.html.erbが適用されない場合の対処法をご教示戴けますと幸いです。
app/views/static_pages/home.html.erb
app/helpers/application_helper.rb
app/views/layouts/application.html.erb
それぞれの提示をお願いします
コメント有難う御座います。
よろしくお願い致します。
【home.html.erb】
<h1>Sample App</h1>
<p>
This is the home page for the
<a href="https://railstutorial.jp/">Ruby on Rails Tutorial</a>
sample application.
</p>
【application.html.erb】
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all',
'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application',
'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= yield %>
</body>
</html>
【application_helper.rb】
module ApplicationHelper
#ページごとの完全なタイトルを返します
def full_title(page_title = '')
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
page_title + '|' + base_title
end
end
end
test/controllers/static_pages_controller_test.rb
もあった方が確実かな、と思いました。
エラーが発生しているファイルですので。
有難う御座います。こちらになります。
【static_pages_controller_test.rb】
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
def setup
@base_title = "Ruby on Rails Tutorial Sample App"
end
test "should get root" do
get root_url
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App"
end
test "should get home" do
get static_pages_home_url
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App"
end
test "should get help" do
get static_pages_help_url
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App"
end
test "should get about" do
get static_pages_about_url
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App"
end
test "should get contact" do
get static_pages_contact_url
assert_response :success
end
end
何度も申し訳ないですが
app/controllers/static_pages_controller.rb
もあると助かります。
使用するlayoutファイルの指定はcontroller内で行えますので。
有難う御座います。
下記のコードになります。
【static_pages_controller.rb】
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
def contact
end
end
ソースコードは一見して問題なさそうなのですが、確かにapplication.html.erbが効いていないか、もしくはhelperが仕事をしていなさそうです。
ブラウザで画面を表示したときのタイトルにはなにが表示されますか?