Come accedo a & # 8220; assegna & # 8221; dopo & # 8220; render: template = > & # 8230; & # 8221 ;?

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

  •  03-07-2019
  •  | 
  •  

Domanda

Ho un metodo di gestione degli errori nel mio ApplicationController:

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

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

In RAILS_ROOT / app / views / errors / not_found.html.erb , ho questo:

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

Ma @exception è sempre zero lì. Ho provato assegnazioni di debug , ma è sempre {} . Le assegnazioni non vengono copiate quando si chiama render: template ? In tal caso, come posso ottenerli?

Sono su Edge Rails.

È stato utile?

Soluzione

È strano e non so perché. In alternativa, hai provato a passare l'eccezione come locale esplicito?

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

e la vista:

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

Altri suggerimenti

Dalla documentazione API per ActionController :: Base sembra come dovresti provare:

render :template => '/errors/not_found', :status => 404, :locals => {:exception => exception}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top