Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a Rails dropdown for related database
    text
    copied!<p>I have 2 models, venues and areas (areas consists of id and name fields). They are related as: one area has many venues and each venue belongs to an area.</p> <p>To assign a venue to an area I am currently entering the area_id number into a text field in the create new venue page. I can then display which area the venue belongs to with:</p> <pre><code>&lt;%= venue.area.name %&gt; </code></pre> <p>Instead of having to enter the ID number of the area in the form I would like to have a dropdown listing the area names for all the area records and for the selected one to be associated with that venue on save.</p> <p>The new venue form:</p> <pre><code>&lt;% form_for @venue do |f| %&gt; &lt;p&gt;name: &lt;br&gt; &lt;%= f.text_field :name %&gt;&lt;/p&gt; &lt;p&gt;address line 1: &lt;br&gt; &lt;%= f.text_field :addressline1 %&gt;&lt;/p&gt; &lt;p&gt;address line 2: &lt;br&gt; &lt;%= f.text_field :addressline2 %&gt;&lt;/p&gt; &lt;p&gt;address line 3: &lt;br&gt; &lt;%= f.text_field :addressline3 %&gt;&lt;/p&gt; &lt;p&gt;area_id: &lt;br&gt; &lt;%= f.text_field :area_id %&gt;&lt;/p&gt; &lt;%= submit_tag %&gt; &lt;% end %&gt; </code></pre> <p>I have tried:</p> <pre><code> &lt;p&gt;area_id: &lt;br&gt; &lt;%= collection_select(:area, :name, @areas, :id, :name) %&gt; </code></pre> <p>But get: </p> <blockquote> <p>You have a nil object when you didn't expect it!<br> You might have expected an instance of Array.<br> The error occurred while evaluating nil.map</p> </blockquote> <p>Any help is much appreciated!</p>
 

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