动态错误模板

时间:2017-10-03 20:30:17

标签: ruby-on-rails

理想情况下,我想要做的是拥有一个通用错误页面,然后在该通用视图中产生特定的错误详细信息。现在我正在这样做 -

accounts_helper.rb -

redirect_to(non_whitelisted_user_path) if condition

non_whitelisted_user.html.erb -

<div class='well col-xs-8 col-xs-offset-2'>
  <i class='fa fa-exclamation-triangle fa-5x col-xs-1 col-xs-offset-5' style='color: red'></i>
  <div class='row'>
    <div class='non_wl_users_subtext col-xs-6 col-xs-offset-3'>
      <h4 class='non_wl_users_header'>Oh, snap!</h4>
      <p>Your institution's quota for Canvas training has been reached. Please contact your administrators for details.</p>
    </div>
  </div>
</div>

但我想做的事情就像 -

布局/ _generic_errror.html.erb -

<div class='well col-xs-8 col-xs-offset-2'>
  <i class='fa fa-exclamation-triangle fa-5x col-xs-1 col-xs-offset-5' style='color: red'></i>
  <%= yield %>
</div>

并能够显示<%= yield %>标记所在的特定错误详细信息。问题是,告诉我的观点在该yield标签中显示x错误的优雅方式是什么?

1 个答案:

答案 0 :(得分:0)

我没有尝试渲染一个yield标签,而是最终做到了这一点 -

accounts_helper.rb -

@error = 'Your institution\'s quota for Canvas training has been reached. '\
'Please contact your administrators for details.'
render template: 'layouts/errors'

errors.html.erb -

<div class='well col-xs-8 col-xs-offset-2'>
  <i class='fa fa-exclamation-triangle fa-5x col-xs-1 col-xs-offset-5' style='color: red'></i>
  <div class='row'>
    <div class='non_wl_users_subtext col-xs-6 col-xs-offset-3'>
      <h4 class='non_wl_users_header'>Oh, snap!</h4>
      <p><%= @error %></p>
    </div>
  </div>
</div>