2012-02-24 60 views
1

我在使用Rails 3.1應用程序中的單表繼承工作時遇到了一些問題。現在我有兩個模型,我正在處理:。我的應用程序包含具有多種不同類型細分的課程。這些段有很多不同的類型,這就是爲什麼我使用單表繼承。我已經能夠確認我的繼承正在通過Rails控制檯工作,但是當我嘗試查看不同的頁面時,我遇到了問題。其中一個頁面位於Lessons#show視圖中。我得到這個錯誤:使用Rails 3.1的單表繼承路由

NoMethodError in Lessons#show 
Showing web/app/views/lessons/show.html.erb where line #21 raised: 
undefined method `web_segment_path' for #<#<Class:0x007f8faf1366b8>:0x007f8faf118320> 

這裏的代碼塊,我得到這個錯誤:

<% @segments.each do |segment| %> 
    <tr> 
    <td><%= segment.title %></td> 
    <td><%= segment.segmenttype %></td> 
     <td><%= segment.type %></td> 
     <td><%= segment.url %></td> 
     <td><%= segment.filepath %></td> 
    <td><%= link_to 'Show', @segment %></td> <!-- This is the line with the error, if I remove this and the following two than everything is displayed properly --> 
    <td><%= link_to 'Edit', edit_segment_path(segment) %></td> 
    <td><%= link_to 'Destroy', segment, confirm: 'Are you sure?', method: :delete %></td> 
    </tr> 
<% end %> 

我很困惑,因爲我從來沒有定義web_segment_path路線,我希望它的路線如前所述,並且只顯示可用的每個段,而不管類型如何。我已經建立了以下型號:

lesson.rb

class Lesson < ActiveRecord::Base 
    belongs_to :course 
    has_many :segments 
end 

segment.rb

class Segment < ActiveRecord::Base 
    TYPE = ['FileSegment','WebSegment','MediaSegment','QuizSegment'] 
    belongs_to :lesson 
end 

web_segment.rb

class WebSegment < Segment 
end 

我知道我缺少的遺產其他類型,但現在我專注於獲取WebSegment類型的工作。有誰知道爲什麼Rails會在我引用@segment時嘗試查找方法'web_segment_path'?就像我說的那樣,如果我刪除指向「顯示」,「編輯」和「刪除」的鏈接,頁面將顯示。爲什麼會這樣?

謝謝你的幫助!

+0

你可以在這裏發佈'耙的輸出routes'這裏(見路線定義了什麼)? – Hck 2012-02-24 09:04:32

+0

當調用rake路由時,我沒有看到任何提及web_segment_path的內容。你還想看看完整的輸出嗎? – Rhawb 2012-02-25 21:08:56

回答

1

好吧我自己想出了這個,這很明顯。我忘了在我的routes.rb文件中做一些簡單的路由。在我的情況下,所有我需要添加了一行:

resources :web_segment, :controller => 'segments'