2013-08-24 2 views
3

이것은 매우 간단하고 문서화가 잘되어 있어야하며 여러 번 해보았습니다. 아직 저를 죽이는 것이 있습니다.중첩 된 부분에 여러 지역 주민을 전달하는 방법

중첩 된 부분을 호출하는 partials 구조가 있습니다. 하나 render 호출이 부분에 여분의 변수를 전달해야 어떤 점에서

는 부분의 렌더링은 실패하더라도 :

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

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

가 여기에 render를 호출 내 코드입니다

이 작동하지 않습니다 불구하고 다음 중 하나를

= f.simple_fields_for :orders do |c| 
    = render "fields", f: c, step: f.object.step 
f.input :quantity, input_html: {step: step} 

form_for는 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 

를 통해 views/orders/_order_forms.html에 전달되는 트레이스 예외입니다 :여기에 예외가 발생 어디

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' 

.

실마리?

내가 사용하고 있습니다 :

ruby 2.0.0p297 
rails 3.2.8 

미리 감사드립니다 많이 있습니다 .. order_forms 두 번 렌더링되고 있음을 내보기를 디버깅 및 낸

UPDATE를의 처음에는 step이 설정되지 않고 두 번째 렌더링에서는 올바르게 설정됩니다.

왜 이런 일이 발생하는지 잘 모르겠지만 다음 줄을 내 fields.html.haml에 추가하여 해결할 수있었습니다.

-step = step || 1 

는 기본적으로 나는 첫 번째 실행에서 렌더링이 충돌하지 않도록 두 번째 실행에서 제대로 작동하는 동안 경우에는, 정의되지 않은 것, 단계 기본값을 넣어.

페이지가 예상대로 나타납니다. 물건을 두 번 렌더링 할 때 자원 낭비를 생각하고 있습니다.

그 이유는 무엇입니까?

+1

f를 전달하는 경우 f.object.step이 필요하지 않습니다. –

+0

예, 고맙습니다.'c'를 호출하고'f'라고 부르면,'f.object'의'step' 만 있으면됩니다. –

+0

'f'는'f.object '를 기반으로 폼 빌더 객체라고 가정합니다. step' 당신의 호출은 아마도'render partial : "fields", locals : {f : f, step : f.object.step}'이어야만 할 것입니다. – vee

답변

4

코드가 두 번 실행되었다는 것을 알게 된 후 다른 사람을 조사하고있었습니다.

나는 누에 고치 기능 물론 link_to_add_association

에 대한 호출이 있었다 단지 몇 줄의 전에 만 부분뿐만 아니라 그 기능을 렌더링 할 필요가 렌더링 공식 render 아니다 것을 깨달았다.

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

link_to_add_association 함수 호출 및 해결 방법을 제거하고 지금은 모든 것이 예상대로 작동합니다

나는 라인을 추가했습니다. 이 유사

더 현대적인 버전은 다음과 같습니다

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

조금 짧은, 그리고 더 나은 찾고.

+0

저는 문자 그대로이 정확한 문제를 가지고 있었고 제 머리를 쓰고있었습니다. 두 번째 로컬은 무시되고있는 것처럼 보였지만 나중에 내 코드에서 내 link_to_add_association이었습니다. 오류를 일으킨 지역을 통과하지 못했습니다! 방금 정의 된 경우 (my_variable) my_variable end'을 내 부분에 추가하여 렌더링 할 때마다 포함되었는지 확인하지 않아도되었습니다. – Cameron

관련 문제