New Discussion

Notifications

You’re not receiving notifications from this thread.

Polymorphic Selects in Rails using GlobalIDs Discussion

4
General

This is fantastic Chris thank you, I'm curious how this would work for the edit view of the form?

You'll need to set the expires_in: nil parameter to have idempotent signed ids generated.
Document.find(5).to_sgid(expires_in: nil)

Documented here https://github.com/rails/globalid

This way your browser will be able to display the currently selected value from the list of options.

Chris, I usually don’t want to leak IDs so I use uuid columns for that, and also change to_param to return the uuid. Do you also think this is a suitable replacement for that use case?

I'm having a hard time figuring out how to set the default value when editing the form. Anyone else have that working?

If it helps anyone else, I figured it out. You have to make sure to add expires_in: nil to your :to_sgid method; otherwise, when you send in the default value, it will use a new expiry, won't match any of the options, and won't mark and display any option as 'selected.'

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 88,834+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.