Moving instance variable from partial to helper I want to display a town name in my navigation, depending on the town I am requesting from the model. The following code works, but I am mixing logic in my view which I know is wrong and I haven't been successful in accessing the @town instance variable from my TownsController.

Can someone lead me down the road to enlightenment in a situation like this? Should I be using a helper? How do I access it in my _header.html.erb partial?

&lt;li class="name"&gt;
   &lt;% @towns = Town.all %&gt;
   &lt;% @towns.each do |t| %&gt;
      &lt;h1&gt;&lt;%= link_to "#{t.name}", '#', id: "logo" %&gt;&lt;/h1&gt;
   &lt;% end %&gt;
&lt;/li&gt;
Is _header.html.erb part of the layout? If so, i would advice you to move this logic to helper, if not all the controllers would need to set the @towns instance variable.

module ApplicationHelper
  def town_names
    Town.all.collect(&amp;:name)
  end
end


And in your view call the town_names. I think you want each name to be in an li element. I have changed the code accordingly.

&lt;% town_names.each do |name| %&gt;
     &lt;li class="name"&gt;
       &lt;h1&gt;&lt;%= link_to name, '#', id: "logo" %&gt;&lt;/h1&gt;
     &lt;/li&gt;
   &lt;% end %&gt;