Skip to content

smsohan/acts_as_permalinkable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

acts_as_permalinkable

acts_as_permalinkable is intended to be a simple don’t repeat yourself way to implement SEO friendly urls for your Ruby on Rails ActiveRecord models. Using this plugin you can generate urls like services/1-car-rental-services-of-Dhaka instead of services/1. Such urls are supposed to be search engine friendly according to google’s SEO starter guide.

Quick Start

Install acts_as_permalinkable plugin: script/plugin install git://github.com/smsohan/acts_as_permalinkable.git

Generate migration: script/generate permalink Service

This will create db/migrate/timestamp_add_permalink_to_services.rb

Run migration: rake db:migrate

In your model:

class Service < ActiveRecord::Base

acts_as_permalinkable

end

In your views:

<%= link_to service_path(service) %>
instead of
<%= link_to service_path(service.id) %>

In your controller:

def show
  @service = Service.find( params[:id] )
end

Usage

The acts_as_permalinkable plugin works in a simple way. It creates a new column ‘permalink’ to you model. And puts an after_create hook to generate the permalink based on a field called ‘name’ or whatever you specify. The permalink always starts with “<model.id>-”. Having this ensures Model.find(params) still works as usual. This works because, the find method does a to_i to its argument and the to_i method by default strips a string to extract only the leading integers.

After the permalink is saved, this is served by overriding the to_param method to return the permalink whenever called by link_to and similmar helper methods.

Configuration options

Using a different field other than the default ‘permalink’:

Generate migration:

script/generate permalink Service my_custom_permalink_field

This will create db/migrate/timestamp_add_my_custom_permalink_field_to_services.rb

In your model:

class Service < ActiveRecord::Base
  acts_as_permalinkable :permalink_field_name => :my_custom_permalink_field
end

Using a different method other than the default ‘name’:

You can use any method of your model that returns a string for generating the permalink. You do not need to take care of escaping or stripping your methods. Just return a string, possibly with spaces.

In your model:

acts_as_permalinkable :permalink_method => :title

or

class Service < ActiveRecord::Base

acts_as_permalinkable :permalink_method => :my_custom_method def my_custom_method ‘my custom permalink text’ end

end

Setting a limit for the length (default is 200 characters) of ‘permalink’:

In your model:

class Service < ActiveRecord::Base

acts_as_permalinkable :length => :100

end

Taking full control over permalinkable text:

At times, you might want to combine a few columns or need some manipulation on your model’s data to generate the permalinkable text. You can do it by overriding the permalinkable_text method. Just return a string from this method, possibly with spaces.

In your model:

class Service < ActiveRecord::Base

acts_as_permalinkable

def permalinkable_text “#{self.id} #{self.title} #{self.service_provider_name} #{self.city}” end

end

About

A ruby on rails ActiveRecord plugin for using search engine (SEO) friendly permalink style URLs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published