I tried rails guide and searched a lot but can find that answer. I am trying generate secret post scaffold in rails 4 by using scaffold generator
Scenario is : users can create secret posts and they will get a link with secret token in future it works as a verification string
rails g scaffold secret title:string content:text token:string
I want to append that token value in all "secret_posts" routes
eg:
: secret/1/sadkljaldjlak
: secret/1/edit/sasadadallkha
I want to use that token as unique code for verification . any help is appreciated
You should be able to do this by specifying the routes individually like this:
get 'secret/:id/:token' => 'secret#show' #or whatever action
In that controller action you can ensure that the token matches the id.
But you should know that this is not secure, that url will be stored in browser history and will be visible to other sites if that page has external links
First, create a migration to chance token:string to token:text, then use the SecureRamdom.urlsafe_base64 to generate the token, use this code on your model:
def secret_token
if self.new_record?
self.token = SecureRandom.urlsafe_base64
end
end
After that, you could create a route like this:
get 'secret/:edit_hash/edit', to: 'secret#edit', as: :secret_offer
patch 'secret/:edit_hash', to: 'secret#update'
put 'secret/:edit_hash', to: 'secret#update'
and on your controller:
def edit
@secret = Secret.find_by!(edit_hash: params[:edit_hash])
your code..
end
def create
@secret = Secret.find_by!(edit_hash: params[:edit_hash])
your code..
end
hope this help
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.