0
votes

I'm wondering how to generate a url that includes friendly ids for two different models. For example, if you have a post titled 'Rails Tutorial' and belonging to a particular User named 'Michael', and they each use a slug in place of their ID, how would you generate a url that looks like site.com/michael/rails-tutorial.

If you set your route to be:

get ':id/:id' => 'posts#show', as: 'show_post'

the URL will either be michael/michael or rails-tutorial/rails-tutorial, yet the way Friendly_id works from what I know is that your slug is represented by :id, so you can't configure it to work with get ':user_id/:post_id'.

I'm sure I just don't understand FriendlyID enough. Any help is appreciated.

1
Check stackoverflow.com/questions/10726665/… . You might get the idea. Try to add some code, showing what you did, this will make it easy for people to help you and you will get more response.Sajan

1 Answers

1
votes

In order to get the url you’re looking for, you’ll need to nest your routes:

resources :users do
  resources :posts
end

This will give you the route:

localhost:3000/users/slug/post/slug

You’ll also have to extend FriendlyId in each model, add the slug to each model in a migration and use .friendly in the controller action(s).