【问题标题】:Getting ActionView::MissingTemplate at / error在/错误处获取 ActionView::MissingTemplate
【发布时间】:2013-12-05 01:34:57
【问题描述】:

我不知道为什么我在这个 Rails 教程中遇到错误。我已经重新启动了一切(计算机、网络砖、崇高的文本等)。任何帮助表示赞赏。

controllers/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>

config/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"
   * "/"

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    “view/visitors/new.html.erb”中的“view”应该是复数,即“views/visitors/new.html.erb”

    【讨论】:

    • 感谢您的回复。仍然得到同样的错误。有什么想法吗?
    • 试试贾斯汀的建议?
    【解决方案2】:

    这是因为您的控制器中的render 'visitors/new' 行。使用visitors/new,它正在寻找文件_new.html.erb 而不是new.html.erb。你应该使用render "new"

    【讨论】:

    • 实际上尝试完全取出渲染“visitors/new”行。因为 Rails 应该默认租用它。
    • 是的,确实如此。我写这个是为了证明
    • 我取出“visitors/new”并收到我在帖子中更新的错误消息。
    • 尝试将视图重命名为 '_new.html.erb'
    【解决方案3】:

    我从头开始,这次成功了。我使用与原始帖子相同的代码。感谢那些有建议的人!

    【讨论】:

      【解决方案4】:

      将“views/visitors/new.html.erb”更改为“views/visitors/new.erb”

      这将停止 Missing Template 错误,因为该模板将用于所有 mime。

      【讨论】:

        猜你喜欢
        • 2013-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-08
        • 1970-01-01
        相关资源
        最近更新 更多