2011-06-12 27 views
0

我想在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是相同的,所以我可以給這兩個獨特的外觀。 我如何糾正這個問題,我是否正確地做到了這一點?

回答

0

奇怪的是,我認爲這是錯誤的,因爲我在使用AptanaStudio 3與git終端。我剛剛重新啓動了一切,現在開始工作,好像數據庫需要時間來刷新自己。所以總結只需重新啓動一切,看看它是否工作。

2

我猜你不知道rake db:migrate:reset是做什麼的。有沒有描述字符串它所以不要問rake它做什麼,你必須look at the source

# desc 'Resets your database using your migrations for the current environment' 
task :reset => ['db:drop', 'db:create', 'db:migrate'] 

所以rake db:migrate:reset破壞你的數據庫(包括你在它有任何的數據),重新創建它,然後應用遷移將所有內容再次更新。但是,您的所有原始數據仍然消失。

db:dropdb:migrate:reset的一部分可能解釋了爲什麼你得到nil到處都是。但是,如果您的所有數據都沒有了,那麼您應該從Price.all獲得一個空數組,以便在重置後添加一些內容。

+1

但是在這種情況下,'Price.all'將是'[]',並且循環'@prices.each do | price |'不會執行,即它不會進入循環。那麼這個錯誤是如何發生的? – rubyprince 2011-06-12 12:12:13

+0

我添加的唯一的東西是類別遷移和搜索遷移。這不可能是原因。 – LearningRoR 2011-06-12 18:38:49

+0

@JustinRoR:你是否已經手動探查SQLite數據庫以查看內容? – 2011-06-12 18:49:54

相關問題