Reversing the effect of slugify()

1,276 views
Skip to first unread message

Ebenezer Olanrewaju

unread,
Nov 2, 2011, 11:23:00 AM11/2/11
to play-fr...@googlegroups.com
Hi everyone,

I have a slugified the title of blog posts in the yabe sample app to form a url. To reverse the effect inside my controller, I replaced all - with space and it worked for now.

But i envisage when the title has other characters like ' which is also replaced with - by slugify, how can I get the correct title back inside the controller?

Thanks.

Ebenezer Olanrewaju

Matt Hildebrand

unread,
Nov 2, 2011, 11:29:48 AM11/2/11
to play-fr...@googlegroups.com
In general, it is not possible to do this from the slug alone -- because, as you point out, multiple strings can map to the same slug.  In other words, the slug function is not invertible.

If you first find the original post in the data repository (via a lookup on the slug or a lookup on a post ID), you can retrieve its proper title from there.



Ebenezer Olanrewaju

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.


Ebenezer Olanrewaju

unread,
Nov 3, 2011, 5:21:54 AM11/3/11
to play-fr...@googlegroups.com
Thanks Matt.

I think i've gotten another idea. I'll slugify each title and save it separately. I'll use this to lookup the post.

Erwan Loisant

unread,
Nov 5, 2011, 5:14:19 AM11/5/11
to play-fr...@googlegroups.com
Be aware of conflicts when you do that, unicity of the slugs is not guaranteed.

I recommend you to include the id in the URL, like that:
https://play.lighthouseapp.com/projects/57987/tickets/1215-add-sorting-to-getchoices

The slug is only here for a better looking URL, but routing is done
purely from the id.

--
Erwan Loisant

Reply all
Reply to author
Forward
0 new messages