An Interest In:
Web News this Week
- March 22, 2024
- March 21, 2024
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
Dynamically direct routes for polymorphic associations
Have you ever struggled with polymorphic associations? We all have, dont worry.
Heres an example of a polymorphic relationship.
Ive recently bumped into a problem where I had to list out all subjects (employees and products in this case) and when user interacted with one list item, it would redirect to that subjects show page.
You can see the problem, right? The obvious solution here would be to check the type of imageable and set the path helper accordingly. But, theres a slightly more advanced technique for this use-case: direct method.
In your routes.rb file simply add this:
And now you can use imageable_path(imageable) as a path helper that will dynamically resolve to imageables show page.
Now our example above becomes:
Aaaand voil, weve got a path helper that resolves to any imageable show page without the need to manually check for the type of imageable.
Original Link: https://dev.to/wizardhealth/dynamically-direct-routes-for-polymorphic-associations-5hhb
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To