Sharing controllers/models within a view

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.

Just Dawson wrote:

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.

the thing is that@service_provider_countris is a instance variable declared in the new action of the ServiceProviderCountries controller

in the new action of the ServiceProviders, you can do @service_provider_countries = ServiceProviderCountries.all

and that will do the work you want.

but a better approach could be to do

<%= f.collection_select :service_provider_country_id, ServiceProviderCountries.all, :id, :name, :include_blank => 'None' %>

Remember to declare the relationship in the model tables and to create the foreing key in the migrations

Rodrigo Dominguez wrote:

Just Dawson wrote:

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.

the thing is that@service_provider_countris is a instance variable declared in the new action of the ServiceProviderCountries controller

in the new action of the ServiceProviders, you can do @service_provider_countries = ServiceProviderCountries.all

and that will do the work you want.

but a better approach could be to do

<%= f.collection_select :service_provider_country_id, ServiceProviderCountries.all, :id, :name, :include_blank => 'None' %>

Remember to declare the relationship in the model tables and to create the foreing key in the migrations

Thank you for your help,

I modified /controllers/service_providers_controller.rb as follows:

  def new     @service_provider = ServiceProvider.new     @service_provider_countries = ServiceProviderCountry.all

    respond_to do |format|       format.html # new.html.erb       format.xml { render :xml => @service_provider }     end   end

This now works as I had hoped. Thank you again.