كيف يمكنني الوصول إلى "تعيينات" بعد "تقديم :template => ..."؟

StackOverflow https://stackoverflow.com/questions/209753

  •  03-07-2019
  •  | 
  •  

سؤال

لدي طريقة لمعالجة الأخطاء في 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 هناك.لقد حاولت debug assigns, ، ولكن هذا دائما {}.لا يتم نسخ التعيينات عند الاتصال render :template؟إذا كان الأمر كذلك، كيف يمكنني الحصول عليها؟

أنا على حافة القضبان.

هل كانت مفيدة؟

المحلول

ذلك غريب، ولا أعرف السبب.كبديل، هل حاولت تمرير الاستثناء باعتباره محليًا صريحًا؟

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

و الرأي :

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

نصائح أخرى

من وثائق API لـ ActionController::Base يبدو أنك يجب أن تحاول:

render :template => '/errors/not_found', :status => 404, :locals => {:exception => exception}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top