Rails的模型继承形式 [英] Rails Model inheritance in forms

查看:149
本文介绍了Rails的模型继承形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了报告制度,我的应用程序。 我创建了一个模型ReportKind例如,但我可以报告了很多东西,我希望让不同群体的报告种。由于他们分享了很多的行为,我想使用继承。

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. Since they share a lot of behavior, I'm trying to use inheritance.

所以我的主要模式:

model ReportKind << ActiveRecord::Base
end

和例如创建的:

model UserReportKind << ReportKind
end

在我的表report_kinds我已经在类型列,并在此之前,它的所有工作。 我的问题是,在表格/控制器。

In my table report_kinds I've the type column, and until here its all working. My problem is in the forms/controllers.

当我做了 ReportKind.new ,我的形式建立与* report_kind *'preFIX。 如果得到的 UserReportKind 的,即使是通过 ReportKind.find 的形式将建设user_report_kind'preFIX。

When I do a ReportKind.new, my form is build with the '*report_kind*' prefix. If a get a UserReportKind, even through a ReportKind.find, the form will build the 'user_report_kind' prefix.

这一切都乱七八糟的控制器,因为有时我会PARAMS [:report_kind],有时PARAMS [: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.

反正是有强制其赠品使用report_kind'preFIX? 此外,我不得不强迫属性类型的控制器,因为它并没有得到直接的形式的价值,有没有pretty的方式做到这一点?

Is there anyway to force it to aways use the 'report_kind' prefix? 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?

路由是另外一个问题,因为它试图建立一个基于在继承车型名称的路线。予克服,通过在路由指向同一控制器添加其他模型

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:

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

这将迫使所有PARAMS为@report_kind是在PARAMS [:report_kind]无论@report_kind是否是UserReportKind与否。此外,所有的岗位,并把请求将转到ReportKindsController为好。

This will force all params for @report_kind to be in params[:report_kind] regardless of whether @report_kind is a UserReportKind or not. Also, all post and put requests will go to the ReportKindsController as well.

其次,你可以用一个隐藏属性像这样指定类型:

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

这将意味着像/ user_report_kinds / ...任何URL将实际使用ReportKindsController。

This will mean that any URL like /user_report_kinds/... will actually use the ReportKindsController.

这篇关于Rails的模型继承形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆