0

AJAX を使用して、nested_attributes_for フォームを含むパーシャルを更新する際に問題が発生しています。

私のアプリケーションは、メイン フォームで選択されたイベント タイプに基づいて、フォームの新しいセクションをロードする必要があります。各タイプに関連付けられたフィールドは、データベース テーブルに格納されます。

現時点では、イベント タイプを選択すると、イベント タイプの ID がリモート関数を介してイベント コントローラに渡されます。(以下のコード)

<%= event_form.collection_select :type_id, Type.find(:all, :order => "event_type"), :id, :event_type, { :prompt => 'Select Event Type' }, { :onchange => 
  remote_function(
     :url => event_specifics_events_path,
     :with => "'type=' + encodeURIComponent(value)" ), } %>

これにより、IDがコントローラーに正常に送信され、イベントタイプIDに関連付けられたすべてのフィールドが検索され、空のdiv(「詳細」のID)が追加フィールドを含むパーシャルに置き換えられます。(以下のコード)

    def event_specifics
    # Catch passed variable and search for event fields by event type id
    selected_type = params[:type]

    @event_fields = EventField.type_id_equals(type)

    if @event_fields
        render :update do |fields|
            fields.replace_html 'specifics', :partial => 'event_specifics', :locals => { :event_form => event_form, :event_fields => @event_fields }
        end
    end
end

私のパーシャルのコードは次のとおりです。

<% if @event_fields %>
<hr 100%>

<% event_form.fields_for :event_specifics do |specifics| %>
    <% for field in @event_fields %>
        <% if field.field_type == "check_box" %>
            <%=h eval "#{@specifics}.#{field.field_type} 'value', {}, '#{field.field_value}', ''" %><strong><span class="pipe"><label><%=h field.field_label %></label></strong>
        <% elsif  field.field_type == "radio_button"  %>
            <%=h eval "#{@specifics}.#{field.field_type} 'value', '#{field.field_value}'" %><strong><span class="pipe"><label><%=h field.field_label %></label></strong>
        <% else %>
            <p>
                <br /><strong><span class="pipe"><label><%=h field.field_label %></label></span><span style="color:#E81E57;">*</span></strong><br />
                <%=h eval "#{@specifics}.#{field.field_type} 'value'" %>
            </p>
        <% end %>
    <% end %>
<% end %>
<% end %>

これを実行しようとすると、エラーが発生します

undefined local variable or method `event_form' for #<ActionView::Base:0x431b2d8>

event_form ビルダーを URL に渡してパラメーターとして処理しようとしましたが、文字列に対して fields_for を呼び出せないと不平を言います。

ビルダー 'event_form' は、親フォームのビルダーです。

<% remote_form_for @event, :url => { :controller => "events", :action => "create" } do |event_form| %>

この問題を解決するアイデアや解決策はありますか? 前もって感謝します!

誰にも役立つ場合に備えて、私は Ruby 1.8.7 と Rails 2.3.8 を Prototype 1.6.0.3 で使用しています。

4

1 に答える 1

1

わかりました。現在、動的フォームが機能しています。代わりにjavascriptを使用してビルドしました。後で誰かがこれに遭遇した場合に備えて、ここに解決策を投稿すると思いました。

コントローラのすべてのイベントフィールドを取得する新しいアクション:

@event_fields ||= EventField.all

パーシャルでは、すべてのフィールドをループしてイベントタイプIDでグループ化し、グループ化されたレコードをループして、フィールドを含むdiv(フィールドが関連付けられているイベントタイプIDのみのID)を作成します。

    <% event_form.fields_for :event_specifics do |specifics| %>
    <% test = @event_fields %>
    <% for field in @event_fields %>
        <div id="<%=h field.type_id %>", style="display:none;", class="specifics_fields">
            <hr 100%>
            <% type_fields = test.select { |t| t.type_id == field.type_id } %>
            <% for type in type_fields %>
                <% if type.field_type == "check_box" %>
                    <%=h eval "specifics.#{type.field_type} 'value', { }, '#{type.field_value}', nil" %><strong><span class="pipe"><label><%=h type.field_label %></label></strong>
                <% elsif  type.field_type == "radio_button"  %>
                    <%=h eval "specifics.#{type.field_type} 'value', '#{type.field_value}'" %><strong><span class="pipe"><label><%=h type.field_label %></label></strong>
                <% else %>
                    <p>
                        <br /><strong><span class="pipe"><label><%=h type.field_label %></label></span><span style="color:#E81E57;">*</span></strong><br />
                        <%=h eval "specifics.#{type.field_type} 'value'" %>
                    </p>
                <% end %>
            <% end %>
        </div>
    <% end %>
<% end %>

ボックスを選択:

<%= event_form.collection_select :type_id, Type.find(:all, :order => "event_type"), :id, :event_type, { :prompt => 'Select Event Type' }, { :onchange => "myFunction(this.value);", } %>

JS機能:

function myFunction(value) {
$$('.specifics_fields').each(function(e){
    e.hide();
});
$(value).show();
}

その場合、Evenythingは意図したとおりに機能し、非常に効率的です。ここから本当に必要なのは、div idを検証するための少しの調整です(div idは数字で始まるべきではありません-簡単に修正できます)。おそらく、テスト変数を削除して、クリーンアップする代わりにすべてをヘルパーに移動する必要があります。景色。

私はこの解決策があまり好きではありませんが、他の方法は見当たらないので、もっとよく知っているなら、遠慮なくこれを行うための新しい方法を提案してください。

于 2010-10-06T13:58:58.087 に答える