0

fields_for 内部でパーシャルをリフレッシュする際に問題があります。

これが部分的なコードです(「table_detalle」)

<table class="table">
    <thead>
        <tr>
            <th><%= t('.denominacion') %></th>
            <th><%= t('.cantidad_ingreso') %></th>
            <th><%= t('.importe') %></th>
        </tr>
    </thead>
    <tbody>

        <%= f.fields_for :operacion_detalles do |builder| %>
        <tr>
            <%= render 'table_detalle_operacion', f: builder %>
        </tr>
        <% end unless @operacion.nil? %>

    </tbody>
</table>
<%= content_tag :h3, t('.total', :value=> number_to_currency(@operacion.R_IMPORTE)).html_safe, :class => 'pull-right', style: 'display:inline'  %>

ユーザーがコンボの値を変更すると、上記の部分を更新したいと思います (詳細オブジェクトを変更する必要があり、編集可能であるため)

JavaScriptコードは次のとおりです。

$('#operacion_TIPOVALOR_ID').change(function(){
                      $.ajax({
                      url: '<%= cambiar_tipo_valor_movimientos_path %>',
                      data: {tipo_valor_id: $('#operacion_TIPOVALOR_ID').val()},
                      complete: function(){
                        $('#tipo_valor_loader').css('display','none');
                      },
                      beforeSend: function(){
                        $('#tipo_valor_loader').css('display','inline');
                      },
                      success: null,
                      dataType: 'script'
                    });
            });

コントローラーコード:

def cambiar_tipo_valor
        @operacion = Operacion.new
        denominaciones = TipoValorDenominacion.all_from_tipo_valor params[:tipo_valor_id]
        denominaciones.each do |deno|
            @operacion.operacion_detalles.build :tipo_valor_denominacion => deno, :I_CANTIDAD => 0, :R_IMPORTE => 0
        end
    end

ユーザーの選択に応じて「operacion_detalles」が変化することがわかります。

.js.erb コード:

$('#detalle').html('<%= escape_javascript(render :partial => 'table_detalle') %>');

しかし、私は得る:

undefined local variable or method `f' for #<#<Class:0x45ae1c8>:0x5062338>

したがって、パーシャルをレンダリングするには f 変数が必要です。f 変数をエミュレートする方法はありますか?

前もって感謝します。

4

1 に答える 1

1

手で問題を解決するしかありませんでした。私が意味したのは?

field_for => each_with_index の代わりに:

    <% @operacion.operacion_detalles.each_with_index  do |detalle, index| %>
        <tr>
        <%= render 'table_detalle_operacion',  detalle: detalle, index: index  %>
        </tr>
    <% end -%>

次のようなタグ ヘルパーを使用します。

<td>
    <input type="hidden" value="<%= detalle.tipo_valor_denominacion.R_MULTIPLICADOR %>" class="multiplicador"/>
    <%= hidden_field_tag "operacion[operacion_detalles_attributes][#{index}][TIPOVALORDENO_ID]", detalle.TIPOVALORDENO_ID %>
    <%= detalle.tipo_valor_denominacion.CTIPOVALORDENO %></td>
<td>
    <%= text_field_tag "operacion[operacion_detalles_attributes][#{index}][I_CANTIDAD]", detalle.I_CANTIDAD %>
    <%= content_tag(:span, detalle.errors[:I_CANTIDAD].join(', '), class: 'help-block') if detalle.errors.has_key? :I_CANTIDAD %>
</td>
<td>
    <%= hidden_field_tag "operacion[operacion_detalles_attributes][#{index}][R_IMPORTE]", detalle.R_IMPORTE %>
    <%= content_tag :span, number_to_currency(detalle.R_IMPORTE), class: 'detalle-importe pull-right' %>
    <%= content_tag(:span, detalle.errors[:R_IMPORTE].join(', '), class: 'help-block') if detalle.errors.has_key? :R_IMPORTE %>
</td>

この場合、私は名前を提供していたので、このコードが実行されると

$('#detalle').html('<%= escape_javascript(render :partial => 'table_detalle') %>')

f 変数は必要ありません。

それが役に立てば幸い。

pd:これはあまり良い解決策ではないことはわかっていますが、うまくいきます。

于 2012-10-16T11:08:38.720 に答える