我有一個部分_errors.html.haml
在我的應用程序中顯示錶單錯誤。部分中的代碼:Rails 4渲染部分本地人
.errors
%ul
- errors.full_messages.each do |message|
%li= message
我從渲染項目/ new.html.haml部分作爲
= render 'shared/errors', locals: { errors: @project.errors } if @project.errors.any?
的誤差部分駐留在views/shared
目錄。
但是當我嘗試渲染錯誤部分時出現錯誤。
undefined local variable or method errors' for #<#<Class:0x0055895405bbc0> :0x00558951a80fe0>
如果我改變渲染行至
= render 'shared/errors', errors: @project.errors if @project.errors.any?
它的工作原理。爲什麼在這種情況下不使用locals
?
我回答了[一個問題](http://stackoverflow.com/questions/38129112/rails-undefined-local-variable-or-method-page/38142541#38142541)也有同樣的問題。你可以查看[render]的來源(https://github.com/rails/rails/blob/8cb8ce98d903929342e2ca3a54a07ab5196baf93/actionview/lib/action_view/helpers/rendering_helper.rb#L26),看看它爲什麼不起作用。 – Thanh