我无法弄清楚为什么我在这个rails教程上遇到错误。我重新启动了所有东西(计算机,网络砖,崇高文本等)。任何帮助表示赞赏。
控制器/ visitors_controller.rb
class VisitorsController < ApplicationController
def new
@owner = Owner.new
render 'visitors/new'
end
end
models / owner.rb
class Owner
def name
name = 'Foobar Kadigan'
end
def birthdate
birthdate = Date.new(1990, 12, 22)
end
def countdown
today = Date.today
birthday = Date.new(today.year, birthdate.month, birthdate.day)
if birthday > today
countdown = (birthday - today).to_i
else
countdown = (birthday.next_year - today).to_i
end
end
end
查看/访问者/ new.html.erb
<h3>Home</h3>
<p>Welcome to the home of <%= @owner.name %>.</p>
<p>I was born on <%= @owner.birthdate %>.</p>
<p>Only <%= @owner.countdown %> days until my birthday!</p>
配置/ routes.rb中
LearnRails::Application.routes.draw do
root to: 'visitors#new'
end
编辑:结束
render 'visitors/new'
现在收到此错误:
ActionView::MissingTemplate at /
Missing template visitors/new with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
* "/Users/waymond/Rails_App/learn-rails/app/views"
* "/Users/waymond/Rails_App/learn-rails"
* "/"
答案 0 :(得分:0)
“view”应为复数,即“views / visitor / new.html.erb”
答案 1 :(得分:0)
这是因为您的控制器中的行render 'visitors/new'
。使用visitors/new
,它正在查找文件_new.html.erb
而不是new.html.erb
。您应该使用render "new"
答案 2 :(得分:0)
我从一开始就开始工作,这一次都有用。我使用了与原始帖子相同的代码。感谢有建议的人!
答案 3 :(得分:0)
将“ views / visitors / new.html.erb”更改为“ views / visitors / new.erb”
由于模板将用于所有哑剧,因此这将停止“丢失模板”错误。