On 5 May 2011, at 07:18, Fearless Fool <[email protected]> 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 [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> 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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to