When we share a Google Drive form, this will give our public URL.
How can we apply it in our Rail application? It should be random and not repeated.
Can anyone help me? Thank you.
Update
I mean this is url:
But I want a url You should add a Permalink
/ model / permalink
, simply add new fields, Save something like secureorendum
and save it to the model, then create the URL and share it. You can do something like:
class SomeModel & lt; ActiveRecord :: Base after_create: gener_permalink Personal DRAF Generated_Permalink self.permalink = SecureRandom.urlsafe_base64 (32) End of End
Then in some views where your user can find permalink url:
The above helpers will create your URL which goes to your some_model
controller's show action. You can, of course, take a new action if you wish and add it to your own routes, but I'm just going through a simple way.
In the show action of your controller you will need to find the model by its permalink:
class SomeModelController & lt; ApplicationController def show @some_model = SomeModel.where ("id =: id or permalink =: id", id: params [: id]). First end end
See more in your routes with a little more tweaking, you can shorten the URL for what you have posted in your question:
routes file to http://yourhost.com/1PPVIMrDo61Er9tqYlJRntfNT73jpxtd_YJGGjXOMlAw
, When no other way is matched, your parametic path will catch the random string and it will be near the controller. Your choice will be:
# config / routes.rb get "/: permalink", for: "some_model # show", like :: permalink
< P> Param [: id] instead of parameter [: permalink]
will be called in its controller here: get "/: id"
You can simplify the code in your controller, but I think it's good to be clear. Then, correct URL to change just your view:
Hope that helps.
Comments
Post a Comment