Ask A Question

Notifications

You’re not receiving notifications from this thread.

Pretty urls with FriendlyID Discussion

Frank Vinci Frank Vinci

Why are you able to use find_by_slug? Are you using the deprecated finders gem?

Reply

It works fine in Rails 4. From what I can tell, those find_by_COLUMN methods didn't go away, but the ones listed in the README here did: https://github.com/rails/ac...

Reply
Wynter Jones Wynter Jones

Awesome. Also cool that you showed process for doing it manually. Sometimes these ruby gems seem like magic if you just use the gem without first trying to do it yourself.

Reply

In real project, should we use integer for userId? How about if 2 different user signup at the same time and who will be assigned 1 or 2?

Reply

Your database will be the one handling those writes and generating the IDs. In most cases like that, you don't need to worry about that but consistency can become a problem when your set up gets a lot larger.

Reply
icsvortex666 icsvortex666

I couldn't make this work with cancancan :(
Any thougts on how to make it work?

Reply

Cancan makes a good amount of assumptions that you are doing the normal Rails lookups here. There are a handful of different ways to make them work together, but the best one I've seen is to not use the load_resource and to authorize in the action instead: https://github.com/norman/f...

Reply

Muchas gracias el tutorial me ayudo mucho lo estoy implementando en un proyecto que tengo jejeje muchas gracias
Thanks very much the tutorial help very well thanks

Reply

Hi, Chris great tutorial ones again. However I have all ready application on the DigitalOcean and now I have to deploy the Friendly_url to it. I use the postgres db so how I can update all records from the table? So the url ID´s will show by name? In the local version sqlite I can you the Profile.find_each(&:save) and it works. But how on the production postgres version?

Reply

This is exactly what I want to talk about in a future episode (basically deploying breaking changes to production).

You have a bunch of options:

A hacky way to do it would be to create a new migration or edit the friendly_id migration to re-save all the profiles. This will do the trick with only one deploy. You shouldn't have any downtime with this approach, but you're heading into territory that can break migrations if you don't do it right. Future you might get frustrated with this hack, but it's certainly the easiest solution.

Another way to do it would be to create a rake task that you can run with Capistrano that runs the profile save. Or simply SSH into the server and run that code in the Rails console after deploy.

The ideal solution (if you need absolutely 0 downtime) is to figure out how half-deploy the update. Basically migrate the database and make save all records (and any new ones) with the new slug code but don't update the controllers to use the new IDs just yet. Once you've deployed that code safely and all the records now have a friendly_id, you can then do a second deploy that changes the URLs everywhere to use the new ones. This is obviously much more involved, but a common practice when you're doing things in production that can't have any downtime.

Get as complicated as you would like to with it, but you're probably fine just deploying the changes and running the save in an SSH rails console if you don't mind a few seconds of possible broken links. That's usually what most people do until they have tons of traffic.

Reply

Say for example I have a reservations model nested inside a rooms model. After implementing friendly_id, my reservations try to save as the room's friendly_id slug instead of the room's ID (integer). How do I make it save by the integer instead?

Thanks!

Reply
Jordano Moscoso Jordano Moscoso

I was thinking about this yesterday, thanks, this is nice feature to make a friendly SEO site

Reply

A great introduction to friendly ids! Worked perfectly even on Rails5.0.0beta3

Reply

Chris, thanks for the video once again and happy thanksgiving!!! I'm getting the following error in the attached screenshot, any ideas? https://uploads.disquscdn.c...

Reply

Looks like possibly @job is nil? Also happy thanksgiving! :D

Reply

How can I solve the problem on parameterizing two-byte characters (ex. Korean, Japanese, Chinese). Would you hint me to solve it?

Reply

Is it possible to to make the url "{id}-{user.fname}-{user.lname}"? It seems to work fine clicking on links, but as soon as i try to create, edit, or delete anything i get route not found.

Reply
Join the discussion
Create an account Log in

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

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

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