Rails 4呈现局部的局部

时间:2022-11-25 20:14:17

I have a partial _errors.html.hamlto display the form errors in my application. The code inside the partial:

我有一个部分_error .html。hamlto在我的应用程序中显示表单错误。代码内部的部分:

.errors
  %ul
    - errors.full_messages.each do |message|
      %li= message

I am rendering the partial from projects/new.html.haml as

我正在渲染项目/new.html的部分内容。haml的

= render 'shared/errors', locals: { errors: @project.errors } if @project.errors.any?

The errors partial resides in views/shared directory.

错误部分位于视图/共享目录中。

But I get an error when I try to render the errors partial.

但是当我试图呈现部分错误时,我得到了一个错误。

undefined local variable or method errors' for #<#<Class:0x0055895405bbc0> :0x00558951a80fe0>

<# <类:0x0055895405bbc0> :0x00558951a80fe0>的未定义局部变量或方法错误'

If I change the rendering line to

如果我将渲染线改为

= render 'shared/errors', errors: @project.errors if @project.errors.any?

it works. Why doesn't using locals work in this case?

它的工作原理。为什么不使用本地语言呢?

2 个解决方案

#1


3  

Just to add on Khanh's answer. I have experimented all the variation. It seems that if you would like to use the term locals for Rails partial rendering, you would need to specify the keyword partial.

再加上Khanh的答案。我试验了所有的变化。似乎如果您想要使用术语local来进行Rails部分呈现,您需要指定关键字partial。

Explicit So this would work

明确,这样就可以了

= render partial: 'shared/errors', locals: { errors: @project.errors } if @project.errors.any?

Implicit Or the short form would be

含蓄的或简短的形式会是

= render 'shared/errors', errors: @project.errors if @project.errors.any?

So in conclusion, if you specify the hash key for rendering partial, all its key has to be specified explicitly. Else you would have to not specify the hash key and let rails figure out implicitly based on the position.

综上所述,如果指定用于呈现部分的哈希键,则必须显式地指定它的所有键。否则,您将不得不不指定哈希键,并让rails基于位置隐式地计算。

#2


0  

I guess that your problem is making condition for locals.

我猜你的问题是当地人的情况。

You can try to do this:

你可以这样做:

- if @project.errors.any?
  = render partial: 'shared/errors', locals: { errors: @project.errors }

In _errors.html.haml

在_errors.html.haml

.errors
  %ul
    - test_errors.full_messages.each do |message|
      %li= message

#1


3  

Just to add on Khanh's answer. I have experimented all the variation. It seems that if you would like to use the term locals for Rails partial rendering, you would need to specify the keyword partial.

再加上Khanh的答案。我试验了所有的变化。似乎如果您想要使用术语local来进行Rails部分呈现,您需要指定关键字partial。

Explicit So this would work

明确,这样就可以了

= render partial: 'shared/errors', locals: { errors: @project.errors } if @project.errors.any?

Implicit Or the short form would be

含蓄的或简短的形式会是

= render 'shared/errors', errors: @project.errors if @project.errors.any?

So in conclusion, if you specify the hash key for rendering partial, all its key has to be specified explicitly. Else you would have to not specify the hash key and let rails figure out implicitly based on the position.

综上所述,如果指定用于呈现部分的哈希键,则必须显式地指定它的所有键。否则,您将不得不不指定哈希键,并让rails基于位置隐式地计算。

#2


0  

I guess that your problem is making condition for locals.

我猜你的问题是当地人的情况。

You can try to do this:

你可以这样做:

- if @project.errors.any?
  = render partial: 'shared/errors', locals: { errors: @project.errors }

In _errors.html.haml

在_errors.html.haml

.errors
  %ul
    - test_errors.full_messages.each do |message|
      %li= message