Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Named Routing in Rails (getting rid of the ids) (See related posts)

Lifted from Semergence
(http://www.semergence.com/archives/2005/12/01/10/02/32/)

In config/routes.rb:

map.projectproject/:projtitle’ :controller =>project’, :action =>show’


In app/controllers/project_controller.rb:
   def show
     @project = Project.find(:first,
                             :conditions => [’title = ?’,params[:projtitle]])end


Or something like that. Assuming your project titles are unique. Then
you can also use project_url (since you have map.project in routes.rb)
for urls in your views, e.g.

   <% for project in @projects %>
   <%= link_to project.title,
               project_url(:projtitle => project.title) %>
    …
   <% end %>

Comments on this post

krails posts on Mar 01, 2006 at 04:33
Very simple and yet helpful. Thanks
randito posts on Mar 07, 2006 at 07:23
i implemented this same kind of functionality as a before_filter in my application.

class BaseController < ApplicationController

before_filter :verify_user, :except => [:search]

def verify_user
@user = User.locate(params[:id])
redirect :action => "error" unless @user
end

You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts