laminatefish
laminatefish

Reputation: 5256

undefined method empty? for nil:NilClass - when it's not nil

Can anyone explain why this code:

    <%= form.select(:language_id) do
            Language.all.each do |lang|
                content_tag(:option, lang.name, value: lang.id,
                    onchange: "setThemeAndMode('#{lang.theme}','#{lang.mode}')")
            end
        end
    %>

Errors with this:

undefined method `empty?' for nil:NilClass
Extracted source (around line #22):
20
21
22
23
24
25

        </p>
    </div>
    <%= form.select(:language_id) do
            Language.all.each do |lang|
                content_tag(:option, lang.name, value: lang.id,
                    onchange: "setThemeAndMode('#{lang.theme}','#{lang.mode}')")

I'm very new to Ruby, and believe it to mean that it isn't finding any languages - But the database is full of them. Removing the content_tag and just having an <h3>lang.name</h3> displays correctly. Any help is very much appreciated.

Upvotes: 1

Views: 100

Answers (1)

AbM
AbM

Reputation: 7779

It is an issue with how you are passing your block. Do this instead

<%= f.select(:language_id) do %>
  <% Language.all.each do |lang| %>
    <%= content_tag(:option, lang.name, value: lang.id,
                     onchange: "setThemeAndMode('#{lang.name}','#{lang.id}')") %>
  <% end %>
<% end %>

Upvotes: 2

Related Questions