我的 Controller 中有一个操作,如下所示:
def show
@post = Post.find_by_setitle(params[:setitle])
if !@post
render 'error_pages/404'
return
end
respond_to do |format|
format.html
end
end
如果render error_pages/404
我得到一个模板丢失。将其切换为 render error_pages/404.haml.html
效果很好。
这是为什么?
注意没有实际的 error_pages Controller 或模型。只是一个方便存放它们的地方。
编辑:我正在使用 mongoid,因此无法访问 ActiveRecord。 Controller 库无法查找特定的 ActiveRecord 异常?
最佳答案
The render method can also use a view that’s entirely outside of your application (perhaps you’re sharing views between two Rails applications):
Rails determines that this is a file render because of the leading slash character. To be explicit, you can use the :file option (which was required on Rails 2.2 and earlier):
您需要传递 :file
选项,或者以斜杠开头位置字符串。或者,您可以使用 Rails 功能来挽救错误,并通过 404 从 ActiveRecord::RecordNotFound
恢复。请参阅 this post了解详情。
关于ruby-on-rails - Rails 3 渲染模板丢失且没有文件结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5022452/