bglusman bglusman - 7 months ago 10
Ruby Question

How do you handle single table inheritance in SimpleForm so a single helper handles all models?

We have a model "EventSession" which has several subtypes via STI, including "NetworkingEventSession" and "DiningEventSession"... we want to be able to handle all of them from one controller and one view in some cases, but simple form is looking at the objects when iterating through in simple_form_for @session and trying to use the networking_event_session_path helper, which we don't currently have defined, instead of the regular event_session_helper path, which would work fine and is what we want.

I could define new routes to get helpers for each subtype, all directing to the same path, but that will be very unDRY, and we may not always want them going to the main event session path... is there some way to override simple_form_for in this particular view to tell it explicitly what model/class to use?

Answer

Specify :url=> networking_event_session_path in the simple_form_for.

Something like this:

<%= simple_form_form @session, :url => networking_event_session_path %>