Rhawb
Rhawb

Reputation: 53

Single Table Inheritance Routing with Rails 3.1

I am having a bit of trouble with getting routing to work with single table inheritance in my Rails 3.1 application. Right now I have two models that I am dealing with: a lesson and a segment. My application consists of lessons that have multiple different types of segments. These segments are of many different types which is why I am using single table inheritance. I have been able to confirm that my inheritance is working through the Rails Console but when I try to view different pages I run into problems. One of these pages is in the Lessons#show view. I get this error:

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>

Here is the chunk of code where I get this error:

<% @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 %>

I am confused because I never defined a route for web_segment_path, and I want it to route to segment as before, and just display every segment available regardless of types. I have set up the following models:

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

I know that I am missing inheritances for other types but right now I am focusing on just getting the WebSegment type working. Does anyone know why Rails would try to find the method 'web_segment_path' when I reference @segment? Like I said, the page will display if I remove the links to "show", "edit", and "delete." Why would this be?

Thank you for any help!

Upvotes: 1

Views: 394

Answers (1)

Rhawb
Rhawb

Reputation: 53

Ok I figured this out myself and it was pretty obvious. I forgot to do some simple routing in my routes.rb file. In my case all I needed to add was one line:

resources :web_segment, :controller => 'segments'

Upvotes: 1

Related Questions