可怕的模板丢失错误

The dreaded missing template error

我已经阅读了我能找到的所有内容,但我仍然感到困惑。

我正在尝试使用 before_filter 来捕获未登录的用户,并将他们发送到另一个索引页面。这个想法是登录的用户在点击 index.html.erb 时会看到他们自己的文章列表,未登录的用户将被重定向到列出文章但没有的 showall.html.erb 页面让他们阅读(并投放一些广告)。

我添加了一条路线:

资源:文章
得到 "showall"
资源:评论
结束

'Rake routes' 显示路线 article_showall。我在 views/articles 文件夹中有部分 _showall.html.erb(它只包含文本 'showall is working!')。如果我在另一个视图中渲染 showall (<%= render "showall" %>),它工作正常。

这是我的控制器的适用部分:

class ArticlesController < ApplicationController
skip_before_action :authorize, only: [:index, :show, :showall]
before_filter :require_user, :only => [:index]

def require_user
unless User.find_by(id: session[:user_id])
  render 'showall', :notice => "Please log in to read articles."
end
end

def index

@articles = current_user.articles

end

def showall
@articles = Article.all
end

当我 运行 它时(虽然没有登录),它得到以下错误:

Missing template articles/showall, application/showall with....etc

我被难住了。为什么我可以在视图中呈现 'showall',但在我的控制器中引用它时出现缺少模板错误?预先感谢您的帮助!

替换

render 'showall', :notice => "Please log in to read articles."

render :file => '/partial/showall', :notice => "Please log in to read articles."

David 和 user4703663 是对的。您的问题是您将模板命名为部分模板,但将其呈现为模板。您可以从文件名中删除下划线并保留代码原样,或者保留文件名原样和索引视图中的 "render partial: 'showall'"。

编辑:我应该补充一点,丢失的模板错误不应该让你心生恐惧。它直接引导你犯错。不要强调它,下次就试着记住它的意思。这几乎总是一个拼写错误,或者加载了一个部分但将其命名为模板,反之亦然,或者忘记了相对路径的子文件夹或类似的东西。这很正常,口译员吐出这些错误是为了帮助您,而不是为了压迫您。

太好了,解决了那个问题,现在又吐了
nil:NilClass

的未定义方法“each”

尽管 'showall' 与 'index' 几乎相同。

叹息