2010-05-19 71 views
2

我目前有兩種模式:廣告系列和視頻。視頻屬於廣告系列,廣告系列包含許多視頻。在我的廣告系列表單中,我希望能夠添加沒有父級的視頻,並且還能夠刪除屬於所選廣告系列的視頻。我想出了兩個單獨的多選列表。一個列表包含所有孤立的視頻,另一個列表包含屬於所選廣告系列的所有視頻。這樣一個用戶,只需選擇添加和刪除哪些視頻。嘗試在我的「更新」和「創建」方法中創建添加和刪除選定Campaign中的視頻的邏輯時遇到了麻煩。我想我需要從每個選擇列表中取一個數組,然後運行一個循環來添加一個循環,並在每個表單中刪除選定的視頻。如何在「has_many」實例中添加和刪除多個「belongs_to」實例?

我會後我從我的形式和我的控制器至今:

廣告活動控制器 - 更新方法:

def update 
    if @campaign.update_attributes(params[:campaign]) 
     unless request.xhr? 
     flash[:notice] = "'#{@campaign.title}' was successfully updated." 
     else 
     flash.now[:notice] = "'#{@campaign.title}' was successfully updated." 
     end 
     unless from_dialog? 
     unless params[:continue_editing] =~ /true|on|1/ 
      redirect_to admin_campaigns_url 
     else 
      unless request.xhr? 
      redirect_to :back 
      else 
      render :partial => "/shared/message" 
      end 
     end 
     else 
     render :text => "<script type='text/javascript'>parent.window.location = '\#{admin_campaigns_url}';</script>" 
     end 
    else 
     unless request.xhr? 
     render :action => 'edit' 
     else 
     render :partial => "/shared/admin/error_messages_for", :locals => {:symbol => :campaign, :object => @campaign} 
     end 
    end 
    end 

活動形式部分:

<%= error_messages_for :campaign -%> 
<% form_for [:admin, @campaign] do |f| -%> 

    <div class='field'> 
    <%= f.label :title -%> 
    <%= f.text_field :title, :class => 'larger' -%> 
    </div> 

    <div class='field'> 
    <%= f.label :description -%> 
    <%= f.text_area :description, :rows => 20, :cols => 140, :class => 'wymeditor' -%> 
    </div> 

    <div class='field'> 
    <%= f.label :date -%> 
    <%= f.date_select :date -%> 
    </div> 

    <div class='field'> 
    <%= f.label :videos_in, "Add Videos" -%> 
    <%= f.collection_select(:title, @orphanedVideos, :id, :title, {}, {:multiple => true}) -%> 
    </div> 

    <div class='field'> 
    <%= f.label :videos_out, "Remove Videos" -%> 
    <%= f.collection_select(:title, @campaignVideos, :id, :title, {}, {:multiple => true}) -%> 
    </div> 

    <div class='field'> 
    <%= f.label :preview -%> 
    <%= render :partial => "/shared/admin/image_picker", :locals => { 
     :f => f, 
     :field => :preview_id, 
     :image => @campaign.preview, 
     :toggle_image_display => false 
    } %> 
    </div> 

    <%= render :partial => "/shared/admin/form_actions", :locals => {:f => f, :continue_editing => false} %> 
<% end -%> 

我m不知道是否正確設置了collection_select(儘管它們在表單上正確顯示)。任何指針將不勝感激。

感謝您的期待!

+0

To c larify,你提交什麼