2

我目前有两个模型:活动和视频。视频属于活动,一个活动有很多视频。在我的广告系列表单中,我希望能够添加没有父级的视频,并且还能够删除属于所选广告系列的视频。我想出了为此使用两个单独的多选列表。一个列表包含所有孤立的视频,另一个列表包含属于所选活动的所有视频。这样,用户只需选择要添加和删除的视频。在我的“更新”和“创建”方法中尝试创建用于从所选广告系列中添加和删除视频的逻辑时,我遇到了麻烦。我想我需要以某种方式从每个选择列表中获取一个数组,然后运行一个添加循环和一个删除每个表单中选定视频的循环。

我将发布到目前为止我的表单和控制器的内容:

广告系列控制器 - 更新方法:

  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 -%>

我不确定 collection_select 的设置是否正确(尽管它们确实在表单上正确显示)。任何指针将不胜感激。

感谢您的关注!

4

1 回答 1

-1

我实际上想出了一个使用复选框的好方法。因此,对于 videos_out 和 videoes_in 部分,我使用了以下代码:

这应该适用于您的默认控制器设置,但您需要确保在没有检查任何项目时通过将其放入控制器中来处理这种情况:

#If no checkboxes checked must create an empty array (whole reason for this function override)
params[:campaign][:video_ids] ||= []
于 2010-06-24T03:26:33.567 回答