I discussed this with Rick this morning. I've written a quick patch
to simply_helpful that will allow you to define a to_params class
method on your model to allow it to change what parameters are passed
to your named routes. This is useful when you have nested resources
map.resources :projects do |project|
What do you guys think?
I've been attacking the same problem myself over the last few days, but
I went for a different approach. Given that you can already use
something like 'edit_thing_path(@project, @thing)' I thought it would be
consistent to use the same kind of approach in form_for, so I've changed
it so you can use 'form_for @project, @thing do ...'.
From a few days experience of working with this approach the main snag
is probably that you really need to have loaded records for each of the
resources in your hierarchy (i.e. projects and things in our example).
Generally that's not bad practice anyway as you'll probably want to
traverse the model to ensure the combination of primary keys is valid,
but there might be reasons not to. In theory you could use 'form_for
@thing.project_id, @thing do ...', but if there's a possibility that the
result of @thing.project_id is a String it becomes impossible to
distinguish this usage of form_for from the original 'form_for
"project", @project, :url => ... do ...'.
FWIW, adding the to_params method to the model feels like the wrong
place to put it to me. The contents of array that you're returning are
determined by the controllers and the routing configuration, so it feels
like we're pushing knowledge into the model that doesn't belong there.
I'd also wonder how it would handle cases where you have the same model
object exposed as a resource with two different nested routes (e.g. the
tags model used as an example in the documentation for the :name_prefix
Hmm; an alternative solution that crosses my mind would be to allow an
array parameter to form_for:
form_for [ @thing.project_id, @thing ] do ...