Rails 3.1 的单表继承路由

发布于 2025-01-08 06:11:47 字数 1879 浏览 0 评论 0原文

我在让路由与 Rails 3.1 应用程序中的单表继承一起使用时遇到了一些麻烦。现在我正在处理两个模型:课程片段。我的应用程序由具有多个不同类型的片段的课程组成。这些段有许多不同的类型,这就是我使用单表继承的原因。我已经能够确认我的继承正在通过 Rails 控制台运行,但是当我尝试查看不同的页面时,我遇到了问题。其中一页位于 Lessons#show 视图中。我收到此错误:

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 类型正常工作。有谁知道为什么当我引用@segment时Rails会尝试查找方法'web_segment_path'?就像我说的,如果我删除“显示”、“编辑”和“删除”的链接,该页面将会显示。为什么会这样呢?

感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

套路撩心 2025-01-15 06:11:47

好吧,我自己发现了这一点,而且非常明显。我忘记在routes.rb 文件中做一些简单的路由。就我而言,我需要添加的只是一行:

resources :web_segment, :controller => 'segments'

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'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文