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

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -