代码之家  ›  专栏  ›  技术社区  ›  Fred Willmore

Rails控制器不呈现任何模板

  •  1
  • Fred Willmore  · 技术社区  · 2 年前

    我有一个关于我开始的rails 7项目的问题。我有一个控制器 HomeController 只需一个动作 home .

    class HomeController < ApplicationController
      def home
        render
      end
    end
    

    我有根路径

     root to: "home#home"
    

    和一个视图文件 app/views/home/home.html.erb

    <% debugger %>
    
    hello this is here
    

    我的 app/views/layouts/application.html.erb 相当标准:

    <% debugger %>
    <!DOCTYPE html>
    <html>
      <head>
        <title>Chargemaster</title>
        <meta name="viewport" content="width=device-width,initial-scale=1">
        <%= csrf_meta_tags %>
        <%= csp_meta_tag %>
    
        <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
        <%= javascript_importmap_tags %>
      </head>
    
      <body>
        hello
        <%= yield %>
      </body>
    </html>
    

    当我跑步时 rails s 然后试着点击主页,我得到了一个空页面。输出告诉我它被处理为HTML,并完成了ok:

    Started GET "/" for ::1 at 2024-06-13 16:29:12 -0700
    Processing by HomeController#home as HTML
    Completed 200 OK in 0ms (Views: 0.1ms | ActiveRecord: 0.0ms | Allocations: 134)
    

    但它不会在任何断点处停止,也不会显示任何文本——中的hello application.html.erb 或者你好,这里 home.html.erb . 渲染的输出如下所示:

    <html>
    <head>
    <meta name="color-scheme" content="light dark"></head>
    <body>
    <pre style="word-wrap: break-word; white-space: pre-wrap;"> </pre>
    </body>
    </html>
    

    为什么此操作没有如我所期望的那样呈现模板?

    如果可以随意查看代码库,请访问 https://github.com/fredwillmore/climate-control .

    1 回复  |  直到 2 年前
        1
  •  1
  •   GProst    2 年前

    这是因为你 ApplicationController 延伸 ActionController::API (即API控制器的轻型版本),而不是 ActionController::Base .

    在您的 app/controllers/application_controller.rb 您应该更改的文件:

    class ApplicationController < ActionController::API
      ...
    end
    

    class ApplicationController < ActionController::Base
      ...
    end