Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are using a <code>text_field</code> for referencing an existing object, a <code>select</code> with Sports as options would be more appropriate here.</p> <p>This is where it has to be changed:</p> <pre><code>&lt;div class="field"&gt; &lt;%= f.label :sport %&gt;&lt;br /&gt; &lt;%= f.text_field :sport %&gt; &lt;/div&gt; </code></pre> <p>To:</p> <pre><code>&lt;div class="field"&gt; &lt;%= f.label :sport %&gt;&lt;br /&gt; &lt;%= f.select :sport_id, options_for_select(Sport.all.map{|s|[s.name, s.id]}) %&gt; &lt;/div&gt; </code></pre> <p>The <code>f.select</code> will generate a select box in HTML, the options will me all the sports in your DB.</p> <p><strong>Some documentation about it</strong>:</p> <ul> <li><a href="http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select">http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select</a></li> <li><a href="http://guides.rubyonrails.org/form_helpers.html#select-boxes-for-dealing-with-models">http://guides.rubyonrails.org/form_helpers.html#select-boxes-for-dealing-with-models</a></li> </ul> <p><strong>A cleaner way</strong> would be to set a variable <code>@sports</code> in your controller and call it then in your views:</p> <pre><code># in controller def edit @sports = Sport.scoped #... # in edit view &lt;div class="field"&gt; &lt;%= f.label :sport %&gt;&lt;br /&gt; &lt;%= f.select :sport_id, options_for_select(@sports.map{ |s| [s.name, s.id] }) %&gt; &lt;/div&gt; </code></pre> <hr> <p><strong>Additionnal information:</strong> If you want to "pre-select" an option for the select, you have to pass it as the second argument of the <code>options_for_select</code> helper:</p> <pre><code>options_for_select(@sports.map{ |s| [s.name, s.id] }, params[:sport_id]) # this will select by default the option that matches the value of params[:sport_id] </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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