2015-04-29 36 views
0

我對如何在Rails中編寫實例變量感到困惑。如何編寫實例變量的代碼困惑

Posted by: <%= link_to(@item.user.username, store_path(@item.user)) %> 
Price: <%= number_to_currency @item.price %> 

VS

Subject: <%= subject_name @item %> 

我只是好奇,是什麼(我)SUBJECT_NAME @item及(ii)@ item.price之間的區別?

它不應該是@ item.subject_name嗎?

+1

'subject_name'會在你的幫手中定義.. – Nithin

+0

'subject_name'可能是[helper method](http://codefol.io/posts/Where-Do-I-Put-My-Code)它是就像其他任何可以在'view'中調用的方法一樣。在'@ item.price'中,'@ item'是'Item'類的實例對象,'price'是該模型的屬性。這就是爲什麼它可以用'dot'符號調用 – kiddorails

+0

只有** subject_name **是Item Item中的一個實例方法,或者它是'items'表中的一個字段,纔可以調用**@item.subject_name**。在這裏,它可能是一個輔助方法。 – Abhi

回答

2

我期望subject_name不是一個變量,而是一種方法。我想你有一個方法subject_name在你的一個助手文件中定義了一個參數 - @item在這個例子中。