如何将多个局部变量传递给嵌套的局部变量 [英] How to pass multiple locals to a nested partial

查看:69
本文介绍了如何将多个局部变量传递给嵌套的局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是非常简单和有据可查的,我已经做过好几次了,尽管有些东西仍然让我很生气.

This should be extremely straightforward and well documented, and I've done it several times, although there's something that's still killing me.

我有一个调用嵌套部分的部分结构.

I have a structure of partials calling nested partials.

在某些时候,一个 render 调用需要向局部传递一个额外的变量,尽管局部渲染失败并显示:

At some point one render call needs to pass an extra variable to the partial, although the rendering of the partial fails with a:

undefined local variable or method `<variable name>' for #<#<Class:....>

这是我调用 render 的代码:

Here's my code for calling the render:

= f.simple_fields_for :orders do |c|
  = render partial: "fields", locals: {f: c, step: f.object.step}

虽然这也不起作用:

= f.simple_fields_for :orders do |c|
  = render "fields", f: c, step: f.object.step

这里是引发异常的地方:

here's where the exception is raised:

f.input :quantity, input_html: {step: step}

form_for 来自 views/lists/_form.html.haml:

the form_for comes from the views/lists/_form.html.haml:

= simple_form_for( @order, :html => { :multipart => true }, defaults: { input_html: { class: 'input-medium' } } ) do |f|

f 然后通过

= render "orders/order_forms", f: f

这是跟踪的例外情况:

    ActionView::Template::Error (undefined local variable or method `step' for #<#<Class:0x007fe0479ba2b0>:0x007fe04256a930>):

application trace:
    app/views/orders/_fields.html.haml:9:in `_app_views_orders__fields_html_haml___1860431911739668171_70300581339300'
    app/views/orders/_order_forms.html.haml:30:in `_app_views_orders__order_forms_html_haml__2241963939037094859_70300612771460'
    app/views/lists/_form.html.haml:48:in `block in _app_views_lists__form_html_haml__1669043093238943449_70300583658680'
    app/views/lists/_form.html.haml:3:in `_app_views_lists__form_html_haml__1669043093238943449_70300583658680'
    app/views/lists/new.html.erb:3:in `_app_views_lists_new_html_erb___1563391577928218041_70300593681100'
    app/controllers/lists_controller.rb:67:in `new'

框架跟踪(结束):

actionpack (3.2.8) lib/action_view/template.rb:145:in `block in render'
activesupport (3.2.8) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.8) lib/action_view/template.rb:143:in `render'
actionpack (3.2.8) lib/action_view/renderer/partial_renderer.rb:265:in `render_partial'
actionpack (3.2.8) lib/action_view/renderer/partial_renderer.rb:238:in `block in render'
actionpack (3.2.8) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.8) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (3.2.8) lib/action_view/renderer/partial_renderer.rb:237:in `render'
actionpack (3.2.8) lib/action_view/renderer/renderer.rb:41:in `render_partial'
actionpack (3.2.8) lib/action_view/helpers/rendering_helper.rb:27:in `render'
haml (4.0.3) lib/haml/helpers/action_view_mods.rb:10:in `block in render_with_haml'
haml (4.0.3) lib/haml/helpers.rb:89:in `non_haml'
haml (4.0.3) lib/haml/helpers/action_view_mods.rb:10:in `render_with_haml'
cocoon (1.2.0) lib/cocoon/view_helpers.rb:40:in `block in render_association'

我以前也遇到过类似的问题,我通过简化当地人的路过来解决了这个问题..但现在我真的很想了解发生了什么.

I've had a similar issue before and I resolved it in simplifying the passing of locals.. but now I would really like to understand what's going on.

有什么线索吗?

我正在使用:

ruby 2.0.0p297
rails 3.2.8

非常感谢..

更新

我调试了我的视图并发现 order_forms 被渲染了两次,第一次 step 没有设置,而在第二次渲染中它被正确设置.

I have debugged my view and figured that the order_forms is being rendered twice, the first time step is not set, while in the second rendering it is set correctly.

我不确定为什么会发生这种情况,但我设法通过将以下行添加到我的 fields.html.haml 来解决这个问题.

I'm not sure why this happens, but I managed to work it around with adding the following line to my fields.html.haml.

-step = step || 1

基本上我给 step 设置了一个默认值,以防它没有定义,这样在第一次执行时渲染不会崩溃,而在第二次执行时它可以正常工作.

basically I put a default value to step, in case it's not defined, so that at the first execution the rendering doesn't crash, while at the second execution it works properly.

页面现在看起来像预期的那样.虽然我在考虑两次渲染这些东西时资源的浪费.

The page looks as expected now. Although I'm thinking about the waste of resources when rendering the stuff twice.

知道为什么会这样吗?

推荐答案

在发现代码运行了两次后,我去调查还有谁在运行它.

After finding out that the code was run twice, I went investigating who else was running it.

然后我意识到在调用 cocoon 函数之前的几行代码link_to_add_association

and I realized that just few lines before there was the call to cocoon function link_to_add_association

当然不仅仅是官方的render渲染部分,还有那个函数需要渲染它.

of course it's not only the official render rendering the partial but also that function needs to render it.

我添加了以下行:

:render_options => {:locals => {:step => step }},

到我的 link_to_add_association 函数调用并删除了解决方法,现在一切都按预期工作.

to my link_to_add_association function call and removed the workaround and now everything works as expected.

与此类似的更现代的版本是:

The more modern version analogous to this would be:

render_options: {locals: {step: step }},

短一点,看起来更好.

这篇关于如何将多个局部变量传递给嵌套的局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆