OK, I am new to rails but trying to learn so my apologies if this question exposes my lack of knowledge!
I have a very simple application with 2 models (Building & Customise) and 2 Controllers (Buildings & Customises) each with an index view.
My root is buildings#index
Within buildings#index view I am trying to access data from both models along the lines of:
<div class="wrap">
<header>
<% @setup.each do |customise| %>
<h1><%= customise.title %></h1>
<% end %>
Where in my buildings controller I have defined:
def index
@buildings = Building.all
@unis = Building.where(buildingtype: "uni")
if params[:parentd].present?
@buildings = Building.where("university = ? OR buildingtype = 'directlet'", params[:parentd])
@setup = Customise.all
end
This yields the following error:
ActionView::Template::Error (undefined method `each' for nil:NilClass):
4:
5: <% end %>
6:
7: <% @unis.each do |uni| %>
8: <option value="<%= uni.name %>"><%= uni.name %></option>
9: <% end %>
app/views/customises/index.html.erb:7:in
`_app_views_customises_index_html_erb__589235851577785880_2190604320'
My reading suggests that accessing data from two models in any view is possible which led me to believe that there is something wrong with my notation.
However, when I try to access the same model information, in the same way but within a view associated with the customises controller the data shows without error.
Can anybody suggest the reason behind this?
In your controller, the if condition is not getting satisfied, so @setup
is nil
. Make sure @setup
gets initialized.