Fearless Fool
2011-May-05  06:18 UTC
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?
-- 
Posted via http://www.ruby-forum.com/.
-- 
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-May-05  20:13 UTC
Re: convincing form_for to generate params for parent class
On 5 May 2011, at 07:18, Fearless Fool <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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? >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> -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.