Rails 3.2.13-从嵌套属性中删除条目 [英] Rails 3.2.13 - Delete Entry From Nested Attributes

查看:80
本文介绍了Rails 3.2.13-从嵌套属性中删除条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Ruby on Rails 3.2.13应用程序,其中有一个collection_select语句. collection_select语句在fields_for语句中,在该语句中,我从collection_select收集选定的ID,然后使用它们填充另一个表.我遇到的问题是,collection_select语句在存储所选ID集合的数组中添加了一个空ID条目.

I have a Ruby on Rails 3.2.13 application where I have a collection_select statement. The collection_select statement is in a fields_for statement where I gather selected ids from the collection_select and use them to populate another table. The problem I am having is that the collection_select statement adds a null id entry in the array that stores the collection of selected ids.

在我看来,这是我的代码:

Here is my code in my view:

<%= f.fields_for :media_topics do |media_topic| %>
  <%= media_topic.label :topic, "Topics" %><%= media_topic.collection_select(:topic_id, Topic.order("name_en"), :id, :name_en, {}, {multiple: true}) %>
<% end %>

以下是选择两个选项后数组外观的示例:

Here is an example of how the array looks after selecting two options:

"media_topics_attributes"=>{"0"=>{"topic_id"=>["", "2", "47"], "id"=>"1895"}}

我通过另一个Stack Overflow成员发现,collection_select中的空白条目是一个自动包含的隐藏值.在Rails 3中,无法不包含该字段.但是根据我对上一个问题的答案中提供的链接,将有一种方法可以在Rails 4中排除该隐藏值.

I have found out through another Stack Overflow member that the blank entry in the collection_select is a hidden value that is automatically being included. In Rails 3 there is no way to not include that field. However according to the link provided in an answer to my previous question Rails 3.2 - collection_select Adding A Null Entry In The First Position Of My Array there will be a way to exclude this hidden value in Rails 4.

在我决定第一次使用accepts_nested_attributes_之前,我跳过了这个值并手动添加了MediaTopic行.我想使控制器代码比现在更整洁.我遇到的问题是弄清楚如何跳过该空白条目.我已经阅读了API和许多其他文章,内容涉及如何跳过此值,但没有一个起作用.

I was skipping this value and adding the MediaTopic rows manually before I decided to use accepts_nested_attributes_for for the first time. I want to make my controller code cleaner than it is right now. The problem I'm having is figuring out how to skip that blank entry. I have read the API and many other posts on how I should be able to skip this value but none of them are working.

这是我在MediaLibrary模型中针对media_topics嵌套属性的代码.

Here is the code I have for the media_topics nested attributes in my MediaLibrary model.

has_many :media_topics, dependent: :destroy
accepts_nested_attributes_for :media_topics, allow_destroy: true, reject_if: proc { |attributes| attributes['id'].blank? }

collection_select中的Topic模型具有字段:id ,该字段将更新模型MediaTopic中的字段:topic_id . MediaTopic中的另一个字段是:media_library_id .我已经在accepts_nested_attributes_for命令中尝试过 id topic_id 字段,但是当我对主要模型进行update_attributes时,我得到一个错误,提示该主题不能为空.我确定错误是由于尝试为数组中的第一个空白条目创建MediaTopic行而引起的.

The Topic model in the collection_select has field :id that will update field :topic_id in model MediaTopic. The other field in MediaTopic is :media_library_id. I have tried field id and topic_id in the accepts_nested_attributes_for command but when I do an update_attributes for the main model I get an error saying that the topic can't be blank. I'm sure the error is coming from trying to create the MediaTopic row for the first blank entry in the array.

我已经研究了几天,但没有尝试过.我检查了API,APIDock和其他来源都无济于事.我想在执行update_attributes之前从media_topics_attributes中删除空白条目.我想我不了解如何编写代码.我已经在API中看到了这个示例,但是我不知道应该去哪里或应该如何编码以摆脱空白值.

I have researched this for several days but nothing I have tried works. I have checked the API, APIDock and other sources to no avail. I want to remove the blank entry from media_topics_attributes before I do update_attributes. I guess I'm not understanding how to write the code. I have seen this in the API as an example but I have no idea where this should go or how I should code this to get rid of the blank value.

params = { member: {
  posts_attributes: [{ id: '2', _destroy: '1' }]
}}

任何帮助将不胜感激.

更新CDT 2013年8月10日晚上10:46

在所有错误检查完成之后,在我的控制器中,我的case语句中有以下when子句.

In my controller I have the following when clause in my case statement after all error checking is complete.

when @media_library.update_attributes(params[:media_library])

更新CDT 2013年8月12日上午8:20

在MediaLibrary模型中,我将accepts_nested_attributes_for更改为以下内容.

In the MediaLibrary model I changed my accepts_nested_attributes_for to the following.

accepts_nested_attributes_for :media_topics, allow_destroy: true

更新CDT 2013年8月12日上午10:50

这是我的调试中哈希的样子:

Here is what the hash looks like in my debug:

media_topics_attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    '0': !ruby/hash:ActiveSupport::HashWithIndifferentAccess
      topic_id:
      - '2'
      - '47'
      id: '1896'

但是我仍然收到错误消息媒体主题主题不能为空.我会继续寻找.

However I still get the error Media topics topic can't be blank. I will keep looking.

更新CDT 2013年8月12日下午12:15

主题是带有名称列表(:id,:name)的模型

Topic is a model with a list of names (:id, :name)

MediaLibrary是我希望能够通过Topic.name选择的行的列表

MediaLibrary is a list of rows that I want to be able to select by Topic.name

MediaTopic是将Topic与MediaLibrary相关的行的列表

MediaTopic is a list of rows that relate Topic to MediaLibrary

MediaLibrary has_many:media_topics,从属::destroy

MediaLibrary has_many :media_topics, dependent: :destroy

主题has_many:media_topics

Topic has_many :media_topics

MediaTopic所属媒体:media_library所属媒体:topic

MediaTopic belongs_to :media_library belongs_to :topic

我正在处理的屏幕将更改MediaLibrary并根据列出主题名称的collection_select语句更新MediaTopic.

The screen I am working on will make changes to MediaLibrary and update MediaTopic depending on a collection_select statement listing Topic names.

推荐答案

根据@vinodadhikary,该数组中的空条目正在Rails 3中按预期方式工作.我最近在Rails 4中重写了该应用程序.首先使用has_many来关联我所有的表.我还仅通过使用collection_select语句替换了fields_for逻辑.我在第一个{}中添加了include_hidden = false,并且null条目未出现在数组中.几天前,我问了一个类似的问题,经过大量搜索,我想出了一个解决方案.详细信息在下面的链接中.

According to @vinodadhikary the null entry in the array is working as expected in Rails 3. I recently rewrote this application in Rails 4. I completely rewrote the logic by first using has_many through to relate all my tables. I also replaced the fields_for logic by just using a collection_select statement. I added include_hidden = false in the first {} and the null entry does not appear in the array. I asked a similar question a few days ago and after a lot of searching I came up with a solution. Details are in the link below.

第4条-使用collection_select获取具有嵌套属性的中间表的ID数组

这篇关于Rails 3.2.13-从嵌套属性中删除条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆