Convincing form_for to generate params for parent class

I have an STI class structure:

class MeteredService < ActiveRecord::Base ; end
class PGEService < MeteredService ; end
class SCEService < MeteredService ; end

In my view, I want update (say) the :resource field:

<%= form_for(metered_service, :url =>
metered_service_path(metered_service)) do |f| %>
<%= f.text_field ‘resource’ %>
<%= f.submit “update” %>
<% end %>

But this generates a params[] hash referring to (e.g.) the PGEService
subclass rather than the MeteredService parent class:

params = {
“utf8”=>“✓”,
“_method”=>“put”,
“authenticity_token”=>“bAKcTUYbLZyGcMJPyf3zEiyjlB8aQlRv4lqZdiwElhE=”,
“pge_service”=>{“resource”=>“gas”},
“commit”=>“update”,
“action”=>“update”,
“controller”=>“metered_services”,
“id”=>“54”}

While I could put code in my metered_services_controller to recognize
the subclass:

def update
@ms = MeteredService.find(params[:id])
if @ms.update_attributes(params[@ms.class.name.underscore])

end

… that doesn’t smell right to me. There must be some
Rails-appropriate trick that will let me write the canonical:

def update
@ms = MeteredService.find(params[:id])
if @ms.update_attributes(params[:metered_service])

end

Am I right? What’s the trick?

On 5 May 2011, at 07:18, Fearless F. [email protected] wrote:

<%= f.text_field ‘resource’ %>
“pge_service”=>{“resource”=>“gas”},
if @ms.update_attributes(params[@ms.class.name.underscore])
end

Am I right? What’s the trick?

You might try form_for :metered_service, metered_service, …

I also have vague memories of overwriting model_name in order to get
sti classes to behave nicely with a single set of resourceful
routes/controllers

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs