ruby-on-rails - 如何在 "assigns"之后访问 "render :template => ..."?

标签 ruby-on-rails ruby actionview

我的 ApplicationController 中有一个错误处理方法:

rescue_from ActiveRecord::RecordNotFound, :with => :not_found

def not_found(exception)
  @exception = exception
  render :template => '/errors/not_found', :status => 404
end

RAILS_ROOT/app/views/errors/not_found.html.erb 中,我有这个:

<h1>Error 404: Not Found</h1>
<%= debug @exception %>

但是 @exception 在那里总是 nil。我试过调试分配,但总是{}。调用 render :template 时不会复制赋值吗?如果是这样,我怎样才能得到它们?

我在边缘 Rails。

最佳答案

这很奇怪,我不知道为什么。作为替代方案,您是否尝试过将异常作为显式本地传递?

def not_found(exception)
  render :template => '/errors/not_found', 
         :status   => 404, 
         :locals   => {:exception => exception}
end

和 View :

<h1>Error 404: Not Found</h1>
<%= debug exception %> <!-- Note no '@' -->

关于ruby-on-rails - 如何在 "assigns"之后访问 "render :template => ..."?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/209753/

相关文章:

ruby-on-rails - 重构以下代码行

Rails 中的 Mysql 约束数据库条目

java - 在 Scala 中,如何像 Ruby 的 .pack (“N” 那样打包 BigInt,将四个字符视为网络字节顺序中的无符号长整型?

ruby - 如何预期失败的步骤并传递 cucumber 中的失败?

ruby-on-rails - rails : Preselect a value in ActionView-Helper 'collection_select'

ruby-on-rails - rails 3.1,为什么使用 Heroku 日志时证书验证失败?

ruby-on-rails - 如何在自定义 gem 中使用 ActiveRecord?

ruby-on-rails - ruby on Rails:使用 form_for 时 ActionView::Template::Error (没有将 Bignum 隐式转换为 String)

ruby - 为什么 #!/usr/bin/env ruby​​ 在 crontab 中不起作用?

ruby-on-rails - 我如何在 Sinatra 中使用 Rails ActionView::Helpers::NumberHelper?