이것은 매우 간단하고 문서화가 잘되어 있어야하며 여러 번 해보았습니다. 아직 저를 죽이는 것이 있습니다.중첩 된 부분에 여러 지역 주민을 전달하는 방법
중첩 된 부분을 호출하는 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
는 기본적으로 나는 첫 번째 실행에서 렌더링이 충돌하지 않도록 두 번째 실행에서 제대로 작동하는 동안 경우에는, 정의되지 않은 것, 단계 기본값을 넣어. 페이지가 예상대로 나타납니다. 물건을 두 번 렌더링 할 때 자원 낭비를 생각하고 있습니다.
그 이유는 무엇입니까?
f를 전달하는 경우 f.object.step이 필요하지 않습니다. –
예, 고맙습니다.'c'를 호출하고'f'라고 부르면,'f.object'의'step' 만 있으면됩니다. –
'f'는'f.object '를 기반으로 폼 빌더 객체라고 가정합니다. step' 당신의 호출은 아마도'render partial : "fields", locals : {f : f, step : f.object.step}'이어야만 할 것입니다. – vee