Sorry if this has been asked before, I'm just looking for a pointer on
the 'standard' or accepted way to get around this issue, as oppose to
some 'bodges' that would seem workable.
Dead simple:
I have an index page for model 'properties' which needs a per-property
link to create a new 'lease'. A link_to is fine for jumping to the
'new' page of the 'lease' model, however I want to pass the
property_id for the lease to the controller, without it appearing in
the url (Presuming I will thus have to POST it)
I know that I could do a javascript function or create a hidden form
but was wondering if there is a more elegant way to solve this, as it
must crop up all of the time.
Well, really, the elegant way is to have the ID in the URL, and GET
it. It's not great practice to use POSTs when a GET would do, simply
for comestic reasons.
Is there a particular business reason you can't have the ID of the
property in the URL? If it's simply that the URL doesn't look pretty
with a querystring attached, you could make 'leases' a nested
resources, so the URLs could look like this:
/properties/1/leases/new
If it's genuinely unacceptable to have the property ID in the URL,
then you will have to send it as form data in a POST, yes. If you want
to avoid JavaScript, but still have the element look like a link
rather than a form submit button, it's possible to style buttons as
links: