2012-06-11 33 views
0

我試圖返回一個值回我的看法,雖然沒有多少成功回報率,如果.. else語句

錯誤

未定義的局部變量或#

方法`時間表」

模型

def win_lose_tie 
    if schedule.for.to_i == schedule.against.to_i then 
     :tie 
    elsif schedule.for.to_i > schedule.against.to_i ? :win : :lose 
    end 
    end 

幫手

def win_lose_tie_display(value) 
    case value 
     when :win 
     "W" 
     when :lose 
     "L" 
     when :tie 
     "T" 
     else 
     "unknown win/lose/tie value" 
    end 
    end 

視圖
<%= win_lose_tie_display(schedule.win_lose_tie) %> 

所有這些返回以下錯誤

NameError附表#索引

顯示/用戶/ ** /站點/ rails_projects/** /應用/視圖/日程安排/ index.html.erb其中行46#上升:

43:   <% @schedules.each do |schedule| %> 
44:    <tr> 
45:     <td><%= schedule.event %></td> 
46:     <td><strong><%= win_lose_tie_display(schedule.win_lose_tie) %></strong> <%= schedule.for %> &mdash; <%= schedule.against %></td> 
47:     <td><%= schedule.opponent %></td> 
48:     <td><%= schedule.date.to_formatted_s(:event_date) %> 
49:     </td> 

任何幫助,這將是偉大的

在此先感謝

+0

您需要提供有關該錯誤的更多信息。點擊Application Trace或Full Trace,前幾行應該提供必要的信息。你的'win_lose_tie'看起來很尷尬。嘗試重構它以擁有更多線條或至少更多大括號。 –

+0

application trace在app_views/schedules/index.html.erb:46:in _app_views_schedules_index_html_erb___1705540283_2185334800' app/views/schedules/index.html中返回以下'app/models/schedule.rb:12:''win_lose_tie' .erb:43:在每個' ' 應用/視圖/時間表/ index.html.erb:43:在'_app_views_schedules_index_html_erb___1705540283_2185334800' 應用程序/控制器/ schedules_controller.rb:7:在'索引' –

+0

@ChristophPetschnig我已經重構了代碼,幷包含一些更多信息 –

回答

0

在你win_lose_tie法,用self交換schedule

這是因爲win_lose_tie是調度對象的實例方法。您已經在計劃對象的上下文中,因此無需編寫schedule.。實際上,那會引用*your_schedule_object*.schedule,它不存在。

作爲網站備註:由於您只讀取值,您甚至可以完全省略self。但是,如果您分配值,則需要self