Rails: call a parent model 's attribute in View? -
here have 2 simple model one-many relation:
class category < activerecord::base attr_accessible :name has_many :items class item < activerecord::base attr_accessible :category_id, :name, :price, :description belongs_to :category and i've view show info item like:
<table> <tr> <td class="field" style="width: 175px;"><b>name:</b></td> <td><%= @item.name %></td> </tr> <tr> <td class="field"><b>price:</b></td> <td><%= @item.price%></td> </tr> <tr> <td class="field"><b>category: </b></td> <td><%= category.find(@item.category_id).name %></td> </tr> <tr> <td class="field"><b>description: </b></td> <td><%= @item.description %></td> </tr> </table> it's work right. here i've question: there's ways call class category 's attribute. @item.category.name try didn't work ( undefined method "name" nil:nilclass)
why have attributes in camel case? downcase them , change this:
<tr> <td class="field"><b>category: </b></td> <td><%= category.find(@item.category_id).name %></td> </tr> to this:
<tr> <td class="field"><b>category: </b></td> <td><%= item.category.name %></td> </tr> it should work.
Comments
Post a Comment