如何添加和删除多个“belongs_to”往返于“has_many”的实例实例?

发布于 2024-09-02 07:14:51 字数 2784 浏览 6 评论 0原文

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

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

营销活动控制器 - 更新方法:

  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 是否设置正确(尽管它们确实在表单上正确显示)。任何指示将不胜感激。

感谢您的关注!

I currently have two models: Campaigns and Videos. Videos belongs to Campaigns and a Campaign has many Videos. In my Campaign form I want to be able to add Videos that have no parent and also be able to remove Videos that belong to the selected campaign. I came up with using two separate multiple selection lists for this. One list has all orphaned Videos and the other has all videos that belong to the selected campaign. That way a user and just select which videos to add and remove. I've run into trouble when trying to create the logic for adding and removing Videos from the selected Campaign in my "update" and "create" methods. I imagine that somehow I would need to take an array from each the selection lists and run a loop that adds and a loop that removes the selected videos in each form.

I'll post what I have so far from my form and my controllers:

Campaigns Controller - Update method:

  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

Campaign Form Partial:

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

I'm not sure if the collection_select's are setup properly (though they do display correctly on the form). Any pointers would be appreciated.

Thanks for looking!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哑剧 2024-09-09 07:14:51

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

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

#If no checkboxes checked must create an empty array (whole reason for this function override)
params[:campaign][:video_ids] ||= []

I actually figured out a nice way using check boxes. So for the videos_out and vidoes_in sections I used the following code:

This should work with your default controller settings, though you'll want to make sure you take care of the scenario when no items are checked by putting this in your controller:

#If no checkboxes checked must create an empty array (whole reason for this function override)
params[:campaign][:video_ids] ||= []
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文