Rails select_tag undefined method `map' for nil:NilClass
<p>I have my problem, kinda solved, but I think its a bit clumsy way, so hope to get a better way, as well as a better understanding whats going on here. </p>

<p><strong>_form.html.erb:</strong></p>

<pre><code> &lt;%= select_tag(:category_id, options_for_select(@categories), :prompt =&gt; "Select one!") %&gt;
</code></pre>

<p><strong>products_controller.rb:</strong></p>

<pre><code>def new
  @product = Product.new
  @categories = Category.all.map { |c| [ c.name, c.id ] }
end
</code></pre>

<p>If user submits form without selecting any of the <code>select_tag</code> options he gets <code>undefined method 'map' for nil:NilClass</code> error. </p>

<p>I know that it comes since my <code>@categories</code> is <code>nil</code>, but I cant figure how to avoid that..? </p>

<p>My final solution, that is working:</p>

<pre><code>&lt;%= select_tag(:category_id, options_for_select(@categories || Category.all.map { |c| [ c.name, c.id ] }), :prompt =&gt; "Select one!") %&gt;
</code></pre>

<p>But I feel there is a better way. Also I think, that by assigning a default <code>select_tag</code> value with <code>:selected</code> might work as well, but I couldnt implement it with my knowledge of Ruby syntax...</p>