All threads / Pretty urls with FriendlyID Discussion
Ask A Question


You’re not receiving notifications from this thread.

Pretty urls with FriendlyID Discussion

Frank Vinci ·

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

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:

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.

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?

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.

Thanks Chris. Then how should we store video's id like Wistia in the database? What is good practice for this? Any suggestion?

I'd suggest using a string column for those since they aren't simple integers.

Thanks Chris. Just the last question on this. How do I dynamically generate the wistia video's id for every single page to make the javascript below work on them?

<script>WistiaEmbed = Wistia.embed("abcd1234", {...});</script>

So on GoRails, I do this inside my episodes/show.html.erb:

wistiaEmbed = Wistia.embed("<%= @episode.video_identifier %>")

Yeah, that what I thought. Chris, thanks a lot for your help! This place is fantastic.

Glad to help and thanks for the kind words. :-)

icsvortex666 ·

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

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:

icsvortex666 ·

Thank you, I'll give it a shot.

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

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?

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.

Thanks Chris for the quick reply. I managet to SQL query the slugs columns.
However now I get issue only on the production version when new object is made. In my case user can add multiple profiles.

So when user create new profile this is the error. In the local machine it works fine.

ActiveRecord::StatementInvalid (PG::NotNullViolation: ERROR: null value in column "id" violates not-null constraint

DETAIL: Failing row contains (null, demottaja, demoo, Helloo, , , , , 2016-01-02 23:04:42.689576, 2016-01-02 23:04:42.689576, null, 1, null, null, 12, null, demoo).

: INSERT INTO "profiles" ("name", "nickname", "description", "facebook", "twitter", "instagram", "linkedin", "location_id", "user_id", "slug", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id"):

app/controllers/profiles_controller.rb:61:in `block in create'

app/controllers/profiles_controller.rb:60:in `create'

I have profile model

extend FriendlyId
friendly_id :nickname, use: :slugged
Here is the repo

Ps:This is my first Rails App so I am learning same time :) The url is

Hmm, I'm not sure about that. I've had that in the past, but usually it's due to some bad column setup on your database table. You might need to fix your profiles table if it doesn't have an ID column for example.

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?


Jordano Moscoso ·

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

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

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...

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

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

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.

Join the discussion

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

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

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

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.