I have 2 related models.
class Store < ActiveRecord::Base
attr_accessible :name, :subdomain
belongs_to :theme
end
class Theme < ActiveRecord::Base
attr_accessible :name, :description, :screenshot_attributes
has_many :stores
end
Everything works fine the problem is when I try to access the theme’s name in the following way.
<% @stores.each do |store| %>
<tr>
<td><%= link_to store.subdomain, store %></td>
<td><%= store.name %></td>
<td><%= store.theme.name %></td>
<td><%= link_to 'Go to Store', root_url(subdomain: store.subdomain) %></td>
<td><%= link_to 'Edit', [:edit, store] %></td>
<td><%= link_to 'Destroy', store, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
This is the line that gets the error.
<td><%= store.theme.name %></td>
And this is the message:
undefined method `name' for nil:NilClass
What is the correct way to access the theme’s name value?
Thankyou!
Just because store.theme is nil. A usefull function is inspect. If you do:
You will see theme_id is nil.