Topic: acts_as_urlnameable

This is thankfully the beginner's section because this is probably a really dumb question smile

What exactly does this plugin do? The documentation basically just says:

Simple usage:
Setting the url name

The usage that will work for 80% of cases is this:

        class Article < ActiveRecord::Base
          acts_as_urlnameable :title

This will format the value of the title attribute of the record to one suitable for use in a URL and save it.

I assumed this meant it would allow a URL like this


to become


Where "my_name" comes from a property on the Model that had acts_as_urlnameable applied to it. Like if we were making a forum and we wanted to look at one member's profile, the url could become "/profile/view/bob", replacing with -- which looks really nice.

But I installed the plugin and played with it, and I couldn't get it to do anything. If it doesn't do what I assumed it does, is there a plugin that does?

Re: acts_as_urlnameable

From the description it sounds like it updates the attribute so it can easily be inserted into a URL (lowercase, underscores, etc.). However, the plugin sure has a lot of code for something so simple. It must do more - no clue what though.

Anyway, if you want named URLs you can override the to_param method in the model. If you have it return the id along with the name it will automatically work with Model.find(params[:id]):

# in model
def to_param

If you don't want the id in there, you'll have to use Model.find_by_name(params[:id]) in the controller or something.

Railscasts - Free Ruby on Rails Screencasts