How to create Random URL?
HI!
There are localhost:3000 my application.. but i want to create a link something like this -> localhost:3000/53843 (开发者_JS百科this is random digit rand(999999)).. How possible i can paste this run into url? and if user enter localhost:3000 he gets an error or redirect to localhost:3000/53843 ?
Ok, so I'll assume you have a redirection model that has an ID (53843) and a redirection_path (google).com.
in your routes file, tell it to have :path=>'' -- otherwise it would expect /redirections/53843
resources :redirections, :path=>''
Then in your controller
class RedirectionsController < ApplicationController
def show
redirection = Redirection.find(params[:id])
redirect_to redirection.redirection_path
end
end
You could create a unique field in your model. When a new object is created, add a method that assigns random string, or number to this field. After that modify routes.rb and controller so that you could return an object by the value of this random field in url.
精彩评论