我想在db:migrate:reset後重新啓動服務器,並且突然我的SQlite3服務器無法啓動。當服務器開始呈現我的datum/index
頁面時,出現錯誤:ActionView::Template::Error (undefined method 'user_id' for nil:NilClass)
。 在我這樣做之前,我有我的數據庫中的實際價格,所以user_id可以檢測到,一切正常,但現在價格走了,我相信它給出了這個錯誤。我該如何修復nil:NilClass的未定義方法`user_id'?
控制器 - Datum
& Price
:
def index
@prices = Price.all
end
查看 - datum/index
& prices/index
:
<h1>Prices</h1>
<table>
<tr>
<th>User</th>
<th>Date</th>
<th>Price name</th>
<th>Price</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @prices.each do |price| %>
<tr>
<td><%= price.user_id %></td>
<td><%= price.date %></td>
<td><%= price.price_name %></td>
<td><%= price.price %></td>
<td><%= link_to 'Show', price %></td>
<td><%= link_to 'Edit', edit_price_path(price) %></td>
<td><%= link_to 'Delete', price, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Price', new_price_path %>
我覺得我這樣做了錯誤的方式,因爲我是新來的Rails。我的目標是複製我的prices/index
視圖,所以我的datum/index
是相同的,所以我可以給這兩個獨特的外觀。 我如何糾正這個問題,我是否正確地做到了這一點?
但是在這種情況下,'Price.all'將是'[]',並且循環'@prices.each do | price |'不會執行,即它不會進入循環。那麼這個錯誤是如何發生的? – rubyprince 2011-06-12 12:12:13
我添加的唯一的東西是類別遷移和搜索遷移。這不可能是原因。 – LearningRoR 2011-06-12 18:38:49
@JustinRoR:你是否已經手動探查SQLite數據庫以查看內容? – 2011-06-12 18:49:54