Possible Duplicate:
Ruby on Rails Callback, what is difference between :before_save and :before_create?
I’m looking to replace Ruby on Rails current id from /1, /2, /3, etc to a randomly generated URL, for example /csd8dsv68dvs.
After looking into creating it, I found this in the post model
before_create { self.code = SecureRandom.hex(5) }
What does the before_create do and how can I implement the random id?
Update
It seems I got one part wrong. I’m not looking to change the ID, I just want to change the URL to a random string that a user won’t be able to guess.
A great gem to help you with alternatives to a resource
:idas the identfier through a route is friendly_id. There is even a RailsCast on it.before_createis an ActiveRecord callback and is well explained both here on SO (as others have commented) and in the API.If you chose to use the raw
You could modify your controller action to find the resource using this code instead of the id.
You’d pass the
:codeattribute to routes for the resource in your viewswhere
resource_pathis the route method for your resource.