ruby-on-rails - Rails 3 渲染模板丢失且没有文件结尾

标签 ruby-on-rails ruby-on-rails-3

我的 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 异常?

最佳答案

来自documentation

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/

相关文章:

ruby-on-rails - Rails 4 模型有效,但不会保存?

jquery - 在 rails 4 上添加 5 星评级

ruby-on-rails - 在 SQlite3 和 PostgreSQL 中使用模型方法

ajax - Rails3 - 使用 Kaminari + AJAX + 不显眼的 javascript 进行排序和分页

ruby-on-rails - 删除 Rails 中的关联模型

ruby-on-rails-3 - Rails3-如何在没有DoubleRender错误的情况下在before_filter中渲染403

ruby-on-rails - Rails 添加具有默认值的列,不影响当前条目

ruby-on-rails - 在 rake 任务中打印到屏幕

JavaScript 作为加载 Rails 表单的条件?

ruby-on-rails - 使用 USR2 重新启动 Unicorn 似乎不会重新加载 production.rb 设置