Rails Model inheritance in forms -
i'm doing reporting system app. created model reportkind example, can report lot of stuff, wanted make different groups of report kinds. since share lot of behavior, i'm trying use inheritance.
so have main model:
model reportkind << activerecord::base end
and created example:
model userreportkind << reportkind end
in table report_kinds i've type column, , until here working. problem in forms/controllers.
when reportkind.new
, form build '*report_kind*' prefix. if userreportkind, through reportkind.find
, form build 'user_report_kind' prefix.
this mess in controllers, since i'll have params[:report_kind], params[:user_report_kind], , on every other inheritance made.
is there anyway force aways use 'report_kind' prefix? had force attribute 'type' in controller, because didn't value direct form, there pretty way this?
routing problem, since trying build routes based in inherited models names. overcome adding other models in routes pointing same controller.
this kind of inheritance tricky. fortunately problems mention solvable.
first, can force forms use specific attribute names , urls this:
<%= form_for :report_kind, @report_kind, :url => report_kind_path(@report_kind) %>
this force params @report_kind in params[:report_kind] regardless of whether @report_kind userreportkind or not. also, post , put requests go reportkindscontroller well.
secondly, can specify type hidden attribute this:
<%= form.hidden_field :type, 'userreportkind' %>
finally, routes, following:
map.resources :user_report_kinds, :controller => :report_kinds
this mean url /user_report_kinds/... use reportkindscontroller.
Comments
Post a Comment