2012-12-16 61 views
0

我爲一個名爲wostatuses的表添加了第二個索引文件。 第一個索引(index.html.erb)工作正常。 但是,第二個指標(index2.html.erb)得到就行了 「而評估nil.each發生錯誤」:Rails評估nil時出現錯誤

<% @wostatuses.each do |wostatus| %> 

該控制器具有:

class WostatusesController < ApplicationController 
# GET /wostatuses 
# GET /wostatuses.json 
def index 
    @wostatuses = Wostatus.order("position") 

    respond_to do |format| 
    format.html # index.html.erb 
    format.json { render json: @wostatuses } 
    end 
end 

的index.html。該局及index2.html.erb有:

<% @wostatuses.each do |wostatus| %> 

在路線,我有:

get "wostatuses/index" 
    get "wostatuses/index2" 

感謝您的幫助!

更新 -

<% Wostatus.find_each do |wostatus| %> worked!!!! 

但是,我需要wostatus.position訂購的記錄。我怎麼做?

+0

@wostatuses爲零;你如何路由index2和填充實例變量? –

+0

對不起,我對Rails很陌生。 「你怎麼路由到index2?」我有上面顯示的路線。 URL是http:// localhost:3000/wostatuses/index2 - 「你如何填充實例變量?」我不確定那是什麼意思。 – Reddirt

+0

我試過<%Wostatus.find.each do | wostatus | %>並得到沒有ID沒有找到Wostatus – Reddirt

回答

0

您必須在您的控制器中爲index2採取相同的代碼爲index1的操作。您還可以製作一個具有通用功能的方法,並在兩個操作中使用該方法來編寫可重用代碼。

+0

感謝您的幫助! – Reddirt