I'm using the Friendly_Id 5 gem in my Rails 4 app to make the URL's "friendlier." I can currently generate URLs like /article/my-article-title. On many blog's today, you also see the date or the id in the path. How would I generate URLs like:
/articles/23/my-article-title
where 23 is the article id. It seems like the slug would actually have to be 23/my-article-title
since slugs have to be unique in the DB. Or how would I generate a date based slug like?
/articles/2014/01/22/my-article-title
I wouldn't put date of the article in the URL, as some people are thrown off by content that isn't created, like, within a week from now, so that might result in a little less traffic for you.
However, I would suggest to put ID next to the title. I'm not sure how to do this with friendly_id, but in my opinion there's a better way. I myself switched from friendly_id slugs to this method.
Simply define to_param
in your model:
def to_param
"#{id}-#{title.parameterize}"
end
Rails will be automatically able to find the id
using Model.find(params[:id])