【问题标题】:index.html.erb is not rendering properly in Railsindex.html.erb 在 Rails 中未正确呈现
【发布时间】:2018-05-07 12:48:31
【问题描述】:

我创建了一个项目,其中 index.html.erb 无法正确呈现,当我使用 application.html.erb 以及删除该文件时一切正常

   Home Controller
   class HomeController < ApplicationController
   end

   Routes file
   Rails.application.routes.draw do
   # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
   root "home#index"
   resources :posts
   end

   application.html.erb
   <!DOCTYPE html>
   <html>
   <head>
   <title>Project</title>
  <%= stylesheet_link_tag    "application" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
 </head>

 <body>
 <%= yield%>
 </body>
</html>

【问题讨论】:

  • “index.html.erb 未正确呈现” - 您能否添加更多详细信息,说明哪些内容无法正常工作?
  • @mikej 这是在添加 application.html.erblink 之后,这是在添加 application.html.erblink 之前
  • 你的路由包含root "home#index",但是你没有在HomeController中定义index方法,即。 def index ....

标签: ruby-on-rails ruby render


【解决方案1】:

您需要在HomeController 中定义您在routes.rb 中引用的index 操作。为此,您必须在 HomeController 中定义一个 index 方法,因此:

# home_controller.rb
class HomeController < ApplicationController
  def index; end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 2022-07-28
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多