Home>

This is the content of Chapter 5 of the Rails tutorial.

I want to display it like an image, but application.html.erb is not applied and only home.html.erb of static_pages is displayed.

My display contents started by rails s (localhost: 3000)

Description of application.html.erb
<! DOCTYPE html>
<html>
<head>
  <title><% = full_title (yield (: title))%></title>
  <% = csrf_meta_tags%>
  <% = csp_meta_tag%>
  <% = stylesheet_link_tag'application', media:'all','data-turbolinks-track':'reload'%>
  <% = javascript_pack_tag'application','data-turbolinks-track':'reload'%>
  <!-[If lt IE 9]>
    <script src = "// cdnjs.cloudflare.com/ajax/libs/html5shiv/r29/html5.min.js">
    </script>
  <! [Endif]->
</head>
<body>
  <header>
      
      <% = link_to "sample app",'#', id: "logo"%>
      <nav>
        <ul>
          <li><% = link_to "Home",'#'%></li>
          <li><% = link_to "Help",'#'%></li>
          <li><% = link_to "Log in",'#'%></li>
        </ul>
      </nav>
      
  </header>
    
    <% = yield%>
    
</body>
</html>
Description of home.html.erb

   Welcome to the Sample App 
  
    This is the home page for the
     Ruby on Rails Tutorial 
    sample application.
  
<% = link_to "Sign up now!",'#', Class: "btn btn-lg btn-primary"%>

<% = link_to image_tag ("rails.svg", alt: "Rails logo", width: "200px"),
                      "https://rubyonrails.org/"%>
Description of application_controller.rb
class ApplicationController</pre>
<pre><code>Description of routes.rb
Rails.application.routes.draw do
  root'static_pages # home'
  get'static_pages/home'
  get'static_pages/help'
  get'static_pages/about'
end



The contents of static_pages_contoroller.rb

What I tried

-I wrote it by looking at the description that it is applied when layout: true is specified in the render description part of application_controller.rb, but it did not change.

def hello
    render html: "hello", layout: true ← This part
  end


・ I reviewed up to Chapter 5 of the Rails tutorial and checked if there were any mistakes in the file description. (I'm copying the file knowing that it's not good because I don't understand the file well.)

-Rails application.html.erb I tried some similar articles without being applied, but I couldn't improve.

-Search rails application.html.erb to reconfirm the role

・ Rails html. erb Search for something that I want to apply → I can't find a good site

What I think is the cause

-Unless layout is specified, application.html.erb is applied by default, and it is recognized that each content of the views file will be entered by the action in the<% = yield%>part (I would appreciate it if you point out if it is wrong). Is the default static_pages home.html.erb? ??

This is my first question, but if you have any advice such as this, I would like to refer to it and improve it, so I would appreciate it if you could tell me.

  • Answer # 1

    What is common in such cases is

    Wrong file name in application.html.erb

    I edited the file but forgot to overwrite it

    After that, there is also a pattern that can be fixed for some reason by restarting rails.

  • Answer # 2

    Due to a description error in application_helpre.rb, an error occurred, so it was not reflected on the layout side.

    Description of application_helper.rb
    Correct description
    module Application Helper
      #Returns the full title per page
      def full_title (page_title ='')
        base_title = "Ruby on Rails Tutorial Sample App"
        if page_title.empty?
          base_title
        else else
          page_title + "|" + base_title
        end
      end
    end


    My wrong description

    module Application Helper
      #Returns a complete other title for each page
      def full_title (page_title = ") ← Syntax error in quotation mark of argument part
        base_title = "Ruby on Rails Tuutorial Sample App"
        if page_title.empty?
          base_title
        else else
          page_title + "|" + base_title
        end
      end
    end


    On the rails tutorial side, it was written in single quotes, but I deleted one double quote and only "" is in the correct state.
    I was talking about'' for single quotes and "" for double quotes, but I was just thinking about filling in as in the example and whether it could be done, and I did not doubt that the basic syntax rules etc. were made. .. (I couldn't even copy it, but ...)
    I'm sorry for making a noise due to a rudimentary mistake, thank you to all the people who answered! !! !! !! !!

  • Answer # 3

    (Removed because it was a misunderstanding)

Related articles