0

I was just wondering how I would make urls theoretically like localhost:3000/:user/post/:post like twitter e.g. https://twitter.com/PigsAndPlans/status/491290440512331776 and how to give the post a unique id of both numbers and letters like vine http://vine.co/v/M2JYAhg3HHY.

Thanks

whyelse
  • 5
  • 2
  • I don't suppose you've read about the [Routing system](http://guides.rubyonrails.org/routing.html) that does exactly this? That and modifying `to_param` (as seen [here](https://gist.github.com/cdmwebs/1209732)) should get you what you need. – PinnyM Jul 21 '14 at 19:20
  • @PinnyM The Routing article certainly helped. I had seen it but only skimmed it and missed the bit I needed haha but for the id I was talking more about the actual generating of the number – whyelse Jul 21 '14 at 19:28
  • That's where `to_param` comes in - see the linked article. – PinnyM Jul 21 '14 at 19:32

2 Answers2

1

Something like this in your routes file will match the first URL pattern

match ":username/post/:post_id", to: "users_posts#index"

and give you params[:username] and params[:post_id] variables in your controller. Think very carefully about putting a variable (:username) as the first part of a route, though. You'll have to make sure your app never needs a URL that will conflict with that pattern. A better approach is the vine URL pattern you give, which has a /v prefix.

James Mason
  • 4,246
  • 1
  • 21
  • 26
0

You can create url pattern with following code in config/routes.rb

resources :users do
  resources :posts
end

# http://localhost:3000/users/:user_id/posts/:post_id
Murtza
  • 1,386
  • 1
  • 19
  • 27
  • @whyelse you can see http://stackoverflow.com/questions/6021372/best-way-to-create-unique-token-in-rails or http://stackoverflow.com/questions/19115929/generating-a-unique-url-with-tokens-in-rails-4-for-an-external-form-response to generate unique token – Murtza Jul 21 '14 at 20:45