Ruby Gem for generation of unique anD not changeable URL - ruby-on-rails

Can someone suggest me the gem in ruby that generates URL which will be unique and not changeable for each individual .

I don't know about a gem, but making a URL non-changable is trivial. Just don't let the users (or developers?) change it. And unique is easy as well, just use the user ID or other unique identifier you have for the user in the URL.
Something like this:
"https://yourdomain.tld/directory/structure/prefix#{user.id}suffix"
The directory structure and prefix/suffix are, of course, optional.
Or use URI scheme, host, etc. instead of hard-coding those bits.

Related

Can friendly-id gem work with capital letters in url e.g. /users/joe-blogs and /users/Joe-Blogs both work

I like the friendly id gem but one problem i'm seeing is when I type in a url with a capitol letter in it such as /users/Joe-Blogs it cant find the page. Its a little trivial but most sites can handle something like this and will generate the page whether it has a capitol letter or not. Does anyone know a fix for this?
Edit: to clarify this is for when users enter a url manually and put capitals in it just because its a name like author/Joe-Blogs. I've seen other sites handle this but rails seems to just give a 404.
friendly_id uses parameterize to create the slugs.
I think the best way to solve your problem is to parameterize the params before using it to find.
# controller
User.find(params[:id].parameterize)
Or parameterize the url where the link originated from.
As an addition to Vic's answer, you'll want to look at url normalization:
The following normalizations are described in RFC 3986 to result in equivalent URLs:
Converting the scheme and host to lower case.
The scheme and host components of the URL are case-insensitive. Most normalizers will convert them to lowercase.
Example: HTTP://www.Example.com/ → http://www.example.com/
In short - it's against convention to use capitalization in your urls.
You may also wish to look at URI normalize; more importantly, you should work to remove the capitalization from your URLs:
URI.parse(params[:id]).normalize

UUIDs & Search Engine Friendly URLs

Our SaaS app, currently in beta, allows users to generate conversations. A conversation's URL currently looks like this:
http://example.com/conversations/view/4c6a4ab4-4795-4a13-a3d9-d9d22cac28e5
I'd like to change the URL to something search engine friendly like this:
http://example.com/conversations/this-is-a-great-conversation-that-you-need-to-join
However, since conversation can potentially have the same title, we'll need the conversation's ID in there. But, that would give us a really nasty looking URL:
http://example.com/conversations/this-is-a-great-conversation-that-you-need-to-join/4c6a4ab4-4795-4a13-a3d9-d9d22cac28e5
Does anyone have any ideas on how to include a smaller unique ID in the URL so that we don't have to include the UUID?
believe me, it's not seo friendly at all:
http://example.com/conversations/this-is-a-great-conversation-that-you-need-to-join
it's more spammy than seo friendly url. since google reduced value of keywords in url
http://searchengineland.com/googles-matt-cutts-on-keywords-in-the-url-16976
keywords in url should not be more that 4 or 5 keywords.
i think you should reduce you url size and depth levels. for instance:
http://example.com/c584501
can work for you. "c" in the beginning means "conversations" you can identify your url by a character without using more levels like:
http://example.com/conversations/584501
the shorter url the more chance of sharing it on social medias.
you can use id of your table in database or use uniqueId if you use php:
http://php.net/manual/en/function.uniqid.php
i usually use my table id and i convert it to base36 with base_convert()
http://php.net/manual/en/function.base-convert.php

Creating a shortened URL for all objects in the database

I would like to display a shortened URL besides the content items on my site for ease of sharing.
What would be the most efficient way of doing so, and are there any suitable gems / libraries?
I am using rails on a mongodb/mongoid stack
should be simple enough (regardless if you are on Mongo / MySQL or anything else). what you need is a small collection (mongo if i may) that holds some kind of an MD5 hash of the real url you are after and the real url itself, for example:
ShortLink.create(:hash_link => Digest::MD5.hexdigest(resource_url(#resource)), :real_link => resource_url(#resource))
I suggest adding another route that catches those like this:
match "l/:key", "ShortLinks#show"
should be easy.
I think you can use bitly gem to shorten your URL.
The following link helps you to configure bitly:
http://www.marketingformavens.com/blog/url-shortening-bitly-ruby-on-rails

is it bad form to have the colon in db and in the URL?

I am designing my namespace such that the id i am storing in the DB is
id -> "e:t:222"
where "e" represents the Event class, "t" represents the type
i am also expecting to use this id in my urls
url -> /events/t:222
Is there anything wrong with doing this?
Is there anything wrong with doing this?
Yes: The colon is a reserved character in URLs that has a special meaning, namely specifying the server port, in a URL.
Using it in other places in the URL is a bad idea.
You would need to URLEncode the colon in order to use it.
There is nothing wrong with doing this, you'll simply need to encode the URL properly. Most libraries with do this automatically for you.
In general though, if you care about your data you shouldn't let the application drive the data or database design. Exceptions to this are application centric databases that have no life outside of a single application nor do you expect to use the data anywhere else. In this case, you may want to stick with schemas and idioms that work best with your application.

How to avoid conflict when not using ID in URLs

I see often (rewritten) URLs without ID in it, like on some wordpress installations. What is the best way of achieve this?
Example: site.com/product/some-product-name/
Maybe to keep an array of page names and IDs in cache, to avoid DB query on every page request?
How to avoid conflicts, and what are other issues on using urls without IDs?
Using an ID presents the same conundrum, really--you're just checking for a different value in your database. The "some-product-name" part of your URL above is also something unique. Some people call them slugs (Wordpress, also permalinks). So instead of querying the database for a row that has the particular ID, you're querying the database for a row that has a particular slug. You don't need to know the ID to retrieve the record.
As long as product names are unique it shouldn't be an issue. It won't take any longer (at least not significant) to look up a product by unique name than numeric ID as long as the column is indexed.
Wordpress has a field in the wp_posts table for the slug. When you create the post, it creates a slug from the post title (if that's how you have it configured), replacing spaces with dashes (or I think you can set it to underscores). It also takes out the apostrophes, commas, or whatnot. I believe it also limits the overall length of the slug, too.
So, in short, it isn't dynamically decoding the URL into the post's title--there's a field in the table that matches the URL version of the post name directly.
As you may or may not know, the URLs are being re-written with Apache's mod_rewrite module. As mentioned here, Wordpress is, in the background, assigning a slug after sanitizing the title or post name.
But, to answer your question, what you're describing is Wordpress' "Pretty Permalinks" feature and you can learn more about it in the Wordpress codex. Newer versions of Wordpress do the re-writing internally (no .htaccess editin, wp_rewrite instead). Which is why you'll see the same ruleset for any permalink structure.
Though, if you do some digging you can find the old rewrite rules. For example:
RewriteRule ^([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$ /index.php?year=$1&monthnum=$2&day=$3 [QSA,L]
Will take a URL like /2008/01/01/ and direct it to /index.php?year=2008&monthnum=01&day=01 (and load a date category).
But, as mentioned, a page like product-name exists only because Wordpress already sanitized the post title and stored it as a field in the database.

Resources