Polymorphic Routes in Rails

Tram Ho

1. Overview

Polymorphic URL helpers are methods for a clever solution to name routes while they are called with just one instance of the Active Record model. They are used in conjunction with ActionController :: Resources.

These methods are useful when you want to create a URL or link to a RESTful resource without having to know the exact type of record in question.

Before Rails 1.2 was released, routes were written in the following way:

redirect_to :controller => "posts", :action => "show", :id => @post.id

Need to specify controller, action, and parameters if any.

In the above example, we will redirect to the PostsController show action with params

Share the news now

Source : Viblo