2010-10-04 62 views
0

我在使用AJAX更新包含nested_attributes_for表單的部分時遇到問題。在使用remote_function和nested_attributes_for傳遞formbuilder時遇到問題

我的應用程序需要根據主窗體中選擇的事件類型加載表單的新部分。與每種類型相關的字段都存儲在數據庫表中。

此刻,當我選擇一個事件類型時,事件類型的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與原型1.6.0.3。

+0

好的,這個問題依然存在,但是我知道我不能將一個formbuilder對象傳遞給控制器​​,它只是一個視圖助手。因此,我不確定我想要的是甚至可能了。 – Verloren 2010-10-05 19:42:21

回答

1

好的,我現在有一個工作的動態表單,我用javascript來構建它。我想我會在這裏發佈我的解決方案,以防以後任何人遇到這種情況。

抓取在控制器上的所有事件字段新的動作:通過分組的記錄建立的div(的ID,其是

@event_fields ||= EventField.all 

在局部迴路通過事件類型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不應該以數字開頭 - 簡單修復),我應該刪除測試變量並將其全部移到幫助程序中,而不是清理風景。

我不太喜歡這個解決方案,但我看不到任何其他的方式,所以請,如果你知道任何更好的,不要猶豫,建議一個新的方法來做到這一點。

相關問題