Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3.1 Rails 3 STI荷载衍生部分_Ruby On Rails 3.1_Single Table Inheritance - Fatal编程技术网

Ruby on rails 3.1 Rails 3 STI荷载衍生部分

Ruby on rails 3.1 Rails 3 STI荷载衍生部分,ruby-on-rails-3.1,single-table-inheritance,Ruby On Rails 3.1,Single Table Inheritance,我想做的是在父类中有一个模板,在默认模板中加载一个分部。但该部分内容应特定于派生类 def类数据项

我想做的是在父类中有一个模板,在默认模板中加载一个分部。但该部分内容应特定于派生类

def类数据项
然后,我们有以下视图结构:

  • app/views/data_item/index.html.erb
  • app/views/data\u item/\u col\u info.html.erb
  • app/views/foo_data_item/_col_info.html.erb
  • app/views/bar_data_item/_col_info.html.erb
在index.html.erb中,我们有如下内容:

item.class.name.underscore

钥匙
价值
{:item=>item}%>
所以,我知道我可以为item对象加载一个partial,但我是一个partial,它不仅仅是为对象加载的,而是一个子partial

一个选项是在views/data\u item/\u col\u info.html.erb中创建一个单独的分部,该分部只有一个if/else块,然后加载一个单独的分部,但我希望有一种更干净的方法来使用STI实现这一点


另外请注意,我不能使用item.type作为生成路径的方法,因为我在视图目录结构中有下划线(type将是foodataitem,但我需要foo_data_item)。

我意识到不能单独使用类的名称来生成路径,但可以这样做:

item.class.name.underscore

这比在每个类中添加
template\u path
方法更方便。来自ActiveRecord,它会将您的camel case类名转换为正确的格式。

因此,这不是我的理想解决方案,但我确实在基类中创建了一个名为
template\u path的函数,该函数会被子类覆盖。我还是不知道,但它至少让我这么做了。