Hello, please forgive me if this answer is obvious! I'm new to Rails this week.
I have two database tables, and the associated scaffolding, controllers, models and views. The first is service_provider, which holds a list of service providers. The second is service_provider_countries which is a table of countries (obvious perhaps).
In views/service_providers/new.html.erb I have a form, that allows the you to create a new service provider (name, country, description).
Which looks like so:
<h1>New Service Provider</h1>
<% form_for(@service_provider) do |f| %> <%= f.error_messages %> <p> <%= f.label :Name %><br /> <%= f.text_field :Name %> </p> <p> <%= f.label :Country %><br /> <%= f.text_field :Country %> </p> <p> <%= f.label :Description %><br /> <%= f.text_field :Description %> </p> <p> <%= f.submit 'Create' %> </p> <% end %>
This works as expected, however, I want to modify the form so it lists all the countries in a select box from the service_provider_countries table.
In views/service_provider_countries/index.html.erb I added:
<%= select ("CountryISO", "id", @service_provider_countries.map {|u| [u.ISO,u.ISO]}) %>
Which works as expected, producing a select box of all the countries.
However, when I copy this bit of code and put it into views/service_providers/new.html.erb it does not work, producing the error:
You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.map
I understand the error to mean that view/service_providers does not have access to the (controller?model?) service_provider_countries, could you please explain how I might do this? Thank you and apologies for the long-winded post.