Easy model urls for Rails
Eric Hodel | Tue, 18 Oct 2005 07:19:00 GMT
One part url params
class Player < ActiveRecord::Base
def url_params
return { :controller => 'players', :action => 'info', :id => username }
end
end
One part url_for override
class ApplicationController < ActionController::Base
def url_for(options, *params)
if options.include? :model then
options = options.delete(:model).url_params.merge options
end
return super(options, *params)
end
end
[Simplified due to some insight from zenspider]
Then just add :model => AR_object to anything that accepts url params:
<ul>
<% @players.each do |player| -%>
<li><%= link_to player.username, :model => player %>
<% end -%>
</ul>
And ba-bam!
<ul>
<li><a href="/players/info/herbert">herbert</a>
<li><a href="/players/info/joseph">joseph</a>
</ul>
Comments are disabled

Articles