简体   繁体   English

Rails模型中的继承模型

[英]Rails Model inheritance in forms

I'm doing a reporting system for my app. 我正在为我的应用做一个报告系统。 I created a model ReportKind for example, but as I can report a lot of stuff, I wanted to make different groups of report kinds. 我创建了一个模型ReportKind,但是由于我可以报告很多东西,我想制作不同类型的报告类型。 Since they share a lot of behavior, I'm trying to use inheritance. 由于他们共享很多行为,我正在尝试使用继承。

So I have the main model: 所以我有主要模型:

model ReportKind << ActiveRecord::Base
end

and created for example: 并创建了例如:

model UserReportKind << ReportKind
end

In my table report_kinds I've the type column, and until here its all working. 在我的表report_kinds中,我是类型列,直到这里它的全部工作。 My problem is in the forms/controllers. 我的问题在于表单/控制器。

When I do a ReportKind.new , my form is build with the '*report_kind*' prefix. 当我执行ReportKind.new ,我的表单使用'* report_kind *'前缀构建。 If a get a UserReportKind , even through a ReportKind.find , the form will build the 'user_report_kind' prefix. 如果获取UserReportKind ,即使通过ReportKind.find ,表单也会构建'user_report_kind'前缀。

This mess everything in the controllers, since sometimes I'll have params[:report_kind], sometimes params[:user_report_kind], and so on for every other inheritance I made. 这会使控制器中的所有内容变得混乱,因为有时候我会有params [:report_kind],有时候是params [:user_report_kind],等等我所做的每一个其他继承。

Is there anyway to force it to aways use the 'report_kind' prefix? 无论如何强迫它使用'report_kind'前缀? Also I had to force the attribute 'type' in the controller, because it didn't get the value direct from the form, is there a pretty way to do this? 此外,我不得不强制控制器中的属性'type',因为它没有直接从表单中获取值,是否有一个很好的方法来做到这一点?

Routing was another problem, since it was trying to build routes based in the inherited models names. 路由是另一个问题,因为它试图基于继承的模型名称构建路由。 I overcome that by adding the other models in routes pointing to the same controller. 我通过在指向同一控制器的路由中添加其他模型来克服这个问题。

This kind of inheritance is always tricky. 这种继承总是很棘手。 Fortunately the problems you mention are all solvable. 幸运的是,你提到的问题都是可以解决的。

First, you can force forms to use specific attribute names and URLs like this: 首先,您可以强制表单使用特定的属性名称和URL,如下所示:

<%= form_for :report_kind, @report_kind, :url => report_kind_path(@report_kind) %>

This will force all params for @report_kind to be in params[:report_kind] regardless of whether @report_kind is a UserReportKind or not. 无论@report_kind是否为UserReportKind,这都会强制@report_kind的所有参数都在params [:report_kind]中。 Also, all post and put requests will go to the ReportKindsController as well. 此外,所有post和put请求也将转到ReportKindsController。

Secondly, you can specify type with a hidden attribute like this: 其次,您可以使用隐藏属性指定类型,如下所示:

<%= form.hidden_field :type, 'UserReportKind' %>

Finally, for routes, I would do the following: 最后,对于路线,我会做以下事情:

map.resources :user_report_kinds, :controller => :report_kinds

This will mean that any URL like /user_report_kinds/... will actually use the ReportKindsController. 这意味着像/ user_report_kinds / ...这样的任何URL实际上都会使用ReportKindsController。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM