Skip to content

Instantly share code, notes, and snippets.

@cblavier
Created November 21, 2010 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cblavier/709079 to your computer and use it in GitHub Desktop.
Save cblavier/709079 to your computer and use it in GitHub Desktop.
Unique token generation
class Event < ActiveRecord::Base
def self.new_with_token params = {}
params[:token] = generate_token
while (Event.find_by_token(params[:token]))
params[:token] = generate_token
end
Event.new(params)
end
def self.generate_token
rand(36**TOKEN_SIZE).to_s(36)
end
def to_param
self.token
end
def self.from_param(token)
Event.find_by_token(token)
end
end
@cblavier
Copy link
Author

Après pas besoin de toucher aux routes, et dans le controlleur associé il suffit de faire Event.from_param(params[:id]) dans l'action show pour charger l'event par son token unique

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment