Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3.2 - collection_select Adding A Null Entry In The First Position Of My Array
    primarykey
    data
    text
    <p>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.</p> <p>Here is my code in my view:</p> <pre><code>&lt;%= f.fields_for :media_topics do |media_topic| %&gt; &lt;%= media_topic.label :topic, "Topics" %&gt;&lt;%= media_topic.collection_select(:topic_id, Topic.order("name_en"), :id, :name_en, {}, {multiple: true}) %&gt; &lt;% end %&gt; </code></pre> <p>Here is an example of how the array looks after selecting two options:</p> <pre><code>"media_topics_attributes"=&gt;{"0"=&gt;{"topic_id"=&gt;["", "2", "47"], "id"=&gt;"1895"}} </code></pre> <p>I would think the array should only have two ids, "2" and "47". The null value is causing a problem with updating my nested attributes because of an error saying that the value can't be blank. When the edit view is displayed for a row with related rows that exists the correct records in the collection_select are selected and highlighted in the list as expected.</p> <p>How do I change the collection_select statement where it does not add the null entry? I do not allow any rows on the MediaTopic model to be added with topic_id equal to null. I have been researching this for several days. I found one where a person had a similar issue but none of the solutions on that question work to solve the problem.</p> <p>Any help would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload