Created
November 21, 2010 19:52
-
-
Save cblavier/709079 to your computer and use it in GitHub Desktop.
Unique token generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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