I’m not sure what Rails elements come into play here, so I’m just
calling this the “store locator problem”. Bear with me…
Assume I have a database-backed Store model. And that it has a
“locator” method that returns a list of NEW store objects – NOT YET
SAVED IN THE DB – that are near a given zip code. (Why? Because the
stores are found via an external service and we don’t want to clog our
db with records we’re never going to use.)
class Store < ActiveModel::Base
Return a (possibly empty) list of instantiated but not yet saved
I’m trying to figure out how best to implement the following
user visits a page. the page contains a form prompting for a zip
user enters zip code and presses “find stores” button
STILL ON THE SAME PAGE, the user is presented with a list of matching
stores with a “choose this one” button (or link) next to each one.
when the user clicks on “choose this one”, that store is saved to the
DB (and a bunch of other stuff happens)
the user is redirected to /stores/nnn, where NNN is the id of the
newly created store.
There’s two catches
write unobtrusive JS later…)
I want all the above steps to be rendered in a partial (e.g.
app/views/stores/_locator.html.erb), so I can reuse it in more than one
place in my system.
I have just about everything working: the stores/_locator partial calls
stores_locator_path => StoresController#locate(zip) =>
Store.locate(zip). Then StoresController calls render(:partial =>
stores/_found_stores) to display the list of stores in place.
My one problem: the StoresController#locate method needs to know where
the request came from, so it knows what page to render (see step 3
Is there a clean way to do this? (I can imagine passing something in
the params list, but that seems messy.) Or is there a better approach