The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Overriding url_for for Fun and Profit

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ryan Davis

Posts: 651
Nickname: zenspider
Registered: Oct, 2004

Ryan Davis is a ruby nerd.
Overriding url_for for Fun and Profit Posted: Dec 2, 2005 3:49 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Ryan Davis.
Original Post: Overriding url_for for Fun and Profit
Feed Title: Polishing Ruby
Feed URL: http://blog.zenspider.com/index.rdf
Feed Description: Musings on Ruby and the Ruby Community...
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Ryan Davis
Latest Posts From Polishing Ruby

Advertisement

A while back I was thinking about MVC and how URLs are built and came to the conclusion that every model should know what its URL is. I don't think it is the responsibility of the Controller to know this, and certainly not the view.

Since Rails uses a Hash to represent a URL, I thought it would be easy to just add a method to every Model that would return a Hash representation of the URL and override url_for to perform the expansion from a model into its URL Hash.

It turns out that it was easy. After some enhancements by Ryan that adds defaults we came up with the following:

class ApplicationController < ActionController::Base
  def url_for(options = {}, *params)
    if options.include? :model then
      model = options.delete(:model)
      defaults = {
        :action => 'show',
        :id => model.id,
        :controller => model.class.name.downcase,
      }
      defaults = defaults.merge model.url_params if model.respond_to? :url_params 
      options.delete :only_path if defaults.include? :only_path
      options = defaults.merge options
    end
        
    return super(options, *params)
  end
end

You can use the modified url_for like this:

@model = Model.find some_id
url_for :model => @model
url_for :model => @model, :action => 'edit'
redirect_to :model => @model
link_to @model.name, :model => @model

Sometimes the defaults aren't good enough though. To override them you add a url_params method to your model. In Trackmap I store enough information about a flickr photo to generate its flickr URL in the database and my url_for modification (with a bit of cheating) makes this very clean:

class Photo < ActiveRecord::Base

  #...

  def url_params
    return { :host => 'flickr.com', :controller => 'photos',
             :action => flickr_nsid, :id => flickr_photo_id,
             :only_path => false }
  end

end

Read: Overriding url_for for Fun and Profit

Topic: My first widget! Previous Topic   Next Topic Topic: Rails is keeping us busy and a book recommendation

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use