Open Source ProjectsCode that Might be Useful to You

Talks I've GivenOn Technologies and Ideas

ThoughtsWhere I Sometimes Write Things

Resume If You Believe In Those

Follow Me On

GitHubIf coding is your thing

TwitterIf you tweet

Fixing the routes for tags

After my first forray into tags, I felt like I needed to further refine and enhance my usage of them.

So we had this code chunk:

# in app/controllers/blog_controller.rb
def tags
  # FIXME do routing magic to change :id to :tag
  @blog_posts = BlogPost.find_tagged_with params[:id]
  render :action => 'all'
# in config/routes.rb
map.connect 'blog/tags/:tag', :controller => 'blog', :action => 'tags'

The issue was that I wanted to be using params[:tag], not params[:id]. The cause was that my map.connect was declared AFTER the default connect string, ie:

# Install the default route as the lowest priority.
map.connect ':controller/:action/:id.:format'
map.connect ':controller/:action/:id'

Apparently I didn’t read the comments closely enough :) Moving it above the default route, it works like I had hoped.

comments powered by Disqus