Rails中缺少模板

时间:2015-10-16 22:37:16

标签: ruby-on-rails ruby

我正在学习Ruby-on-Rails。为此我有一个'hello world'样品。 那是我的config/routes.rb

Rails.application.routes.draw do
   root 'application#show'
end

controllers/application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  def show
    render 'startpage'
  end
end

最后我的views/startpage.html.erb

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Hello World!</title>
</head>

<body id="index" class="home"><h1>Hello World!!</h1>
</body>
</html>

但是当我打开我的网站时,我收到了这个错误:

Template is missing
Missing template application/startpage with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/home/ubuntu/workspace/app/views"

那么,我错过了什么或者在其他地方有startpage.html.erb文件吗?

1 个答案:

答案 0 :(得分:1)

您的模板需要位于/views/application内才能使约定生效。

Rails按惯例查看/views/CONTROLLER/action内部而不指定特定位置。

/views/application/startpage.html.erb应该适用于您的情况。

奖励学习要点:

我在创建普通静态页面时看到的常见做法是PagesController,它处理每个页面。这样做,您的路线将指向pages#startpage,您的视图将位于/views/pages/startpage.html.erb内 - 只是一个提示!

相关问题