Rails Model inheritance in forms
I'm doing a reporting system for my app. I created a mo开发者_StackOverflowdel 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.
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. My problem is in the forms/controllers.
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.
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.
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) %>
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
This will mean that any URL like /user_report_kinds/... will actually use the ReportKindsController.
精彩评论