Adding title to rails route - ruby-on-rails

I have List objects which are shown like this:
www.mysite.com/lists/123
Where 123 is the id of the list. What I would like to do is add the title of the list the url so it it more informative(for google or whatever). So I would like it to look like:
www.mysite.com/lists/123/title-of-list-number-123
How do you go about adding to a url like this? If you just enter:
www.mysite.com/lists/123 w/o the title, should it find the title and then redirect to a new route?

If you want to keep your find-calls as they are (by id), you could do the opposite of what mplacona suggested:
def to_param
"#{id}-#{title.parameterize}"
end
With this, your find(params[:id]) will work because it'll convert the string to an integer (can only succeed if the number is in the beginning of the string). So this is will actually work:
List.find("123-my-title")
and will be the same as
List.find(123)
Read more about this and other ways to accomplish this here: http://gregmoreno.ca/how-to-create-google-friendly-urls-in-rails/
The parameterize will automatically convert the string to a "pretty" url. Read more here: http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html#M001367
If you want a bit more functionality, I'll suggest friendly_id aswell.

This article says exactly what you need to do accomplish this.
http://railscasts.com/episodes/63-model-name-in-url
UPDATE
Have a permalink added to your model, and save as follow to it:
def to_param
"#{permalink}-#{id}"
end
On your controller, instead of getting things by the id, get them by the pemalink:
#product = Product.find_by_permalink(params[:id])
And that's all you need.
The screen cast explains all the steps on how to do it.

You could also take a look at friendly id, if you're in the mood for a gem/plugin.

Related

what is the best way to get a portion of this url

I am creating like a link like so:
<%= link_to('', "#{issueable}/#{order.id}/issues" %>
which creates a link like this:
root/version2/parts/2418/issues
I want to be able to get the "parts" (/version2/parts/2418/issues) portion of that url when the user clicks the link to that controller method.
You can use split:
link = "root/version2/parts/2418/issues"
puts link.split('/')[2].strip
#outputs parts
link = "version2/parts/2418"
puts link.split('/')[1].strip
#outputs parts
Ruby fiddle
In other words, you've got a string "/version2/parts/2418/issues" and you want to extract the 'parts' position from it.
"/version2/parts/2418/issues".split('/')[-3]
You'll need to figure out yourself whether to get [2] from the split array or [-3]. As an added '/' in the beginning or in the end could mess it up.
You could use regex assuming you know that it starts with "version2" and also know the order_id when it gets to it.
"/version2/parts/2418/issues".match(/\/version2\/(\w+)\/2418/)
puts $1

Rails to_param - Without ID

I want to create param /users/will-smith, so here's my code:
def to_param
"#{full_name.parameterize}"
end
Parameterize will convert "Will Smith" to "will-smith"
So in the controller, the param won't match the find statement, thus return nil
# User with full_name "will-smith" not found
#user = User.find_by_full_name(params[:id])
Most of the solutions I found is by changing the param to #{id}-#{full_name.parameterize}. But I don't want the URL to contain the ID.
The other solutions is adding permalink column in the database, which I don't really like.
Any other solution?
Thanks
Here's a gem called FriendlyId. It will give you more options to play with.
The problem with your code is that you need to convert that parameter back to original, or use the same kind of transformation on your column during the search. FriendlyId, basically, helps you to achieve the same effect.
Also, I'm not sure, but you could miss that gist. It contaits lots of info on the topic.

Couchdb finder using CouchRest

I just want to know how can I build a find_all_by_action_and_author_id method in Rails with while using the couchdb. My Model looks like this:
class Activity < CouchRest::Model::Base
property :action, String
property :author_id, String
end
if I try to build a View like that:
design do
view :by_action_and_author_id
end
I dont know how to get the right result, I tried it with this:
Activity.by_action_and_author_id(:keys => [['action','foo'], ['author_id', '1']]).all
But the result is always a empty hash. What is the best way to do this? Any examples?
With PostgreSQL it would look like this
Activity.where(action: 'foo', author_id: '1').all
it cant be so complicated
Thanx
have a look at the generated couchdb-view! you can see which keys get emitted. there is no thing as a mapping there, so i think that [['action','foo'], ['author_id', '1']] should be just ['foo', '1'].
i do not know for sure how couchrest-models handles views, but you can find more infos here: http://www.couchrest.info/model/view_objects.html
have a look at the tag-example.

Which characters in a search query does Google ignore (versus treating them as spaces?)

I want to give my pages human-readable slugs, but Rails' built-in parameterize method isn't SEO-optimized. For example, if I have a post called "Notorious B.I.G. is the best", parameterize will give me this path:
/posts/notorious-b-i-g-is-the-best
which is suboptimal since Google construes the query "Notorious B.I.G." as "Notorious BIG" instead of "Notorious B I G" (i.e., the dots are removed rather than treated as spaces)
Likewise, "Tom's fave pizza" is converted to "tom-s-fave-pizza", when it should be "toms-fave-pizza" (since Google ignores apostrophe's as well)
To create a better parameterize, I need to know which characters Google removes from queries (so I can remove them from my URLs) and which characters Google treats as spaces (so I can convert them to dashes in my URLs).
Better still, does such a parameterize method exist?
(Besides stringex, which I think tries to be too clever. 2 representative problem cases:
[Dev]> "Notorious B.I.G. is the best".to_url
=> "notorious-b-dot-i-g-is-the-best"
[Dev]> "No, Curren$y is the best".to_url
=> "no-curren$y-is-the-best"
I would try using a gem that has been designed for generating slugs. They often make good design decisions and they have a way of updating the code for changing best practices. This document represents Google's best practices on URL design.
Here is a list of the best gems for solving this problem. They are sorted by rank which is computed based on development activity and how many people "watch" changes to the gems source code.
The top one right now is frendly_id and it looks like it will generate good slugs for your use in SEO. Here is a link to the features of the gem. You can also configure it and it looks like it is perfect for your needs.
Google appears to have good results for both the "b-i-g" and "big" in the url slugs.
For the rails side of things, yes a parameterize method exists.
"Notorious B.I.G. is the best".parameterize
=> "notorious-b-i-g-is-the-best"
I think you can create the URLs yourself... something like
class Album
before_create :set_permalink
def set_permalink
self.permalink = name.parameterize
end
def to_params
"#{id}-#{permalink}"
end
end
This will create a url structure of:
/albums/3453-notorious-b-i-g-is-the-best
You can remove the id section in to_params if you want to.
Use the title tag and description meta tag to tell google what the page is called: these carry more weight than the url. So, leave your url as /posts/notorious-b-i-g-is-the-best but put "Notorious B.I.G. is the best" in your title tag.

How to make Urls in Rails seofriendly

How can i realize seo friendly urls?
Instead
http://mysite.com/articles/show/2
i would like to use the articlename instead the id
i.e.
mysite.com/articles/show/articlename
or somehow combine id and articlename like this
mysite.com/articles/show/articlename-2
i'm a rails newbie so perhaps you could give me short advice where to change
something with what code?
Look in your article controller, probably in app/controllers/articles.rb. You probably have a method named show which looks up an article by id with something like this:
#article = Article.find(params[:id])
If you know the id is going to be the title of the post instead of its id, you can instead look up your article using
#article = Article.find_by_title(params[:id])
This will allow you to use somewhat ugly URLs like /articles/show/This+is+the+title. If you want to make a slightly nicer URL, you could add a column to your article table (called, say, seo_title) to store the title translated to lowercase with underscores, yielding something like this_is_the_title.
#and
Your question is at once both simple and yet difficult. Best you check out this more mature Stack Overflow post to find your answer:
https://stackoverflow.com/questions/723765/how-do-i-make-the-urls-in-ruby-on-rails-seo-friendly-knowing-a-vendor-name/
It has more examples and more options. While find_by_title is an option, it is far from your best option.

Resources