我正在使用单表继承并对所有子类都有注释.我只为所有不同的STI类型使用1个控制器.当form_for帮助程序为子类型生成URL时,它会尝试使用子类型的帮助程序,但我希望它使用父类的帮助程序.
这是我得到的错误:
undefined method `subclasstypename_comments_path' for #<ActionView::Base:0x41ef27c>
Run Code Online (Sandbox Code Playgroud)
它应该使用的路径助手是
parentclasstypename_comments_path
Run Code Online (Sandbox Code Playgroud) 假设我有一个带有3个模型的rails应用程序,Person,Place和Thing.假设Thing使用单表继承,因此有FancyThing和ScaryThing子类.然后有定义的路线map.resources :people, :places, :things.所以没有FancyThings和ScaryThings的控制器,ThingsController处理任何类型.
现在说我需要有代码,显示任何有链接的列表.如果我在我的视图中有这个代码:
<% @items.each do |item| %>
<%= link_to item.name, item %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
如果item是Person或Place,则工作正常,polymorphic_path负责生成正确的路由.但是如果item是FancyThing或ScaryThing,这会爆炸,因为它会尝试使用fancy_thing_path,没有路由.我想以某种方式使它使用thing_path.理想情况下,Thing和/或其子类上会有一个方法以某种方式指示子类应该使用基类来生成路由.有一个优雅的解决方案吗?
我已经为person类实现了单表继承
class Person < ActiveRecord::Base
end
class Teacher < Person
end
class Student < Person
end
class Outsider < Person
end
Run Code Online (Sandbox Code Playgroud)
而创建人似乎根据form.select中选择的内容创建了Teacher,Student或Person,并添加了type属性.
但是,我似乎打破了路线
<%= link_to'Edit',edit_person_path(@deal)%> | <%= link_to'Back',persons_path%>
它们似乎指向teacher_path,student_path和outsider_path而不是person_path.
路线需要做哪些改变?
ruby routing ruby-on-rails single-table-inheritance ruby-on-rails-3
我正在使用STI,我想知道,我是否必须为每个型号配备一个单独的控制器?我有一种情况,我只在STI关系中使用一个模型的创建和编辑操作,但如果我尝试为其执行表单,则会出现"未定义的方法"错误.更具体地说,我有两个继承自List的模型:
class RegularList < List
class OtherList < List
Run Code Online (Sandbox Code Playgroud)
我有一个处理这些操作的列表控制器,但我只使用表单创建带有RegularList的新模型.即我使用form_for创建新的List对象的唯一情况是使用RegularList.我想做的是:
class ListsController < ApplicationController
def new
@list = RegularList.new
end
Run Code Online (Sandbox Code Playgroud)
否则创建新列表的路径看起来像regular_list/new,但我希望它只是list/new.思考?
编辑:问题是当我使用上面的代码时,我得到一个'未定义的方法'错误.我的观点如下:
...所以在Lists控制器中使用RegularList对象似乎存在一些问题,这是我试图解决的主要问题.对不起,我意识到这不是最清楚的解释.