# Sharing controllers/models within a view

**URL:** <https://www.ruby-forum.com/t/sharing-controllers-models-within-a-view/172296>\
**Category:** Rails\
**Created:** [July 18, 2009, 8:38pm UTC](https://www.ruby-forum.com/t/sharing-controllers-models-within-a-view/172296 "2009-07-18T20:38:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dawson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac91a4/32.png) [@dawson](https://www.ruby-forum.com/u/dawson)\
**Post date:** [July 18, 2009, 8:38pm UTC](https://www.ruby-forum.com/t/sharing-controllers-models-within-a-view/172296/1 "2009-07-18T20:38:36Z")

</div>

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:

# New Service Provider

\<% form\_for(@service\_provider) do |f| %\>  
\<%= f.error\_messages %\>

\<%= f.label :Name %\>  
 \<%= f.text\_field :Name %\>

\<%= f.label :Country %\>  
 \<%= f.text\_field :Country %\>

\<%= f.label :Description %\>  
 \<%= f.text\_field :Description %\>

\<%= f.submit 'Create' %\>

\<% 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.

---

<div class="post-metadata">

**Author:** ![dawson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac91a4/32.png) [@dawson](https://www.ruby-forum.com/u/dawson)\
**Post date:** [July 18, 2009, 9:20pm UTC](https://www.ruby-forum.com/t/sharing-controllers-models-within-a-view/172296/2 "2009-07-18T21:20:57Z")

</div>

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:
> 
> # New Service Provider
> 
> \<% form\_for(@service\_provider) do |f| %\>  
> \<%= f.error\_messages %\>
> 
> \<%= f.label :Name %\>  
> \<%= f.text\_field :Name %\>
> 
> \<%= f.label :Country %\>  
> \<%= f.text\_field :Country %\>
> 
> \<%= f.label :Description %\>  
> \<%= f.text\_field :Description %\>
> 
> \<%= f.submit 'Create' %\>
> 
> \<% 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

---

<div class="post-metadata">

**Author:** ![dawson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac91a4/32.png) [@dawson](https://www.ruby-forum.com/u/dawson)\
**Post date:** [July 18, 2009, 9:26pm UTC](https://www.ruby-forum.com/t/sharing-controllers-models-within-a-view/172296/3 "2009-07-18T21:26:13Z")

</div>

Rodrigo D. 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:
> > 
> > # New Service Provider
> > 
> > \<% form\_for(@service\_provider) do |f| %\>  
> > \<%= f.error\_messages %\>
> > 
> > \<%= f.label :Name %\>  
> > \<%= f.text\_field :Name %\>
> > 
> > \<%= f.label :Country %\>  
> > \<%= f.text\_field :Country %\>
> > 
> > \<%= f.label :Description %\>  
> > \<%= f.text\_field :Description %\>
> > 
> > \<%= f.submit 'Create' %\>
> > 
> > \<% 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.
