technicalpickles

Open Source ProjectsCode that Might be Useful to You

Talks I've GivenOn Technologies and Ideas

ThoughtsWhere I Sometimes Write Things

Resume If You Still Believe In Those

Follow Me On

GitHubIf coding is your thing

TwitterIf you tweet

TumblrIf you're ADD

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'
end
    ...
    
# 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