From c769ad85336713b7e5bdadd57d92a007783f8208 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 12 May 2007 21:12:31 +0000 Subject: Removed deprecated parameters_for_method_reference concept (legacy from before named routes) [DHH] Added record identification with polymorphic routes for ActionController::Base#url_for and ActionView::Base#url_for [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6729 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_view/helpers/url_helper.rb | 50 +++++++++++++++++------- 1 file changed, 36 insertions(+), 14 deletions(-) (limited to 'actionpack/lib/action_view/helpers/url_helper.rb') diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index ca7e9872cc..2329fb4bc6 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -29,6 +29,13 @@ module ActionView # * :password -- Inline HTTP authentication (only plucked out if :user is also present) # * :escape -- Determines whether the returned URL will be HTML escaped or not (true by default) # + # ==== Relying on named routes + # + # If you instead of a hash pass a record (like an Active Record or Active Resource) as the options parameter, + # you'll trigger the named route for that record. The lookup will happen on the name of the class. So passing + # a Workshop object will attempt to use the workshop_path route. If you have a nested route, such as + # admin_workshop_path you'll have to call that explicitly (it's impossible for url_for to guess that route). + # # ==== Examples # <%= url_for(:action => 'index') %> # # => /blog/ @@ -47,15 +54,30 @@ module ActionView # # <%= url_for(:action => 'checkout', :anchor => 'tax&ship', :escape => false) %> # # => /testing/jump/#tax&ship - def url_for(options = {}, *parameters_for_method_reference) - if options.kind_of? Hash + # + # <%= url_for(Workshop.new) %> + # # relies on Workshop answering a new_record? call (and in this case returning true) + # # => /workshops + # + # <%= url_for(@workshop) %> + # # calls @workshop.to_s + # # => /workshops/5 + def url_for(options = {}) + case options + when Hash options = { :only_path => true }.update(options.symbolize_keys) - escape = options.key?(:escape) ? options.delete(:escape) : true - else + escape = options.key?(:escape) ? options.delete(:escape) : true + url = @controller.send(:url_for, options) + when String escape = true + url = options + when NilClass + url = @controller.send(:url_for, nil) + else + escape = false + url = polymorphic_path(options, self) end - url = @controller.send(:url_for, options, *parameters_for_method_reference) escape ? html_escape(url) : url end @@ -104,7 +126,7 @@ module ActionView # f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href; # var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); # m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Delete Image - def link_to(name, options = {}, html_options = nil, *parameters_for_method_reference) + def link_to(name, options = {}, html_options = nil) if html_options html_options = html_options.stringify_keys convert_options_to_javascript!(html_options) @@ -113,7 +135,7 @@ module ActionView tag_options = nil end - url = options.is_a?(String) ? options : self.url_for(options, *parameters_for_method_reference) + url = options.is_a?(String) ? options : self.url_for(options) "#{name || url}" end @@ -222,8 +244,8 @@ module ActionView # link_to("Go back", { :controller => 'posts', :action => 'index' }) # end # %> - def link_to_unless_current(name, options = {}, html_options = {}, *parameters_for_method_reference, &block) - link_to_unless current_page?(options), name, options, html_options, *parameters_for_method_reference, &block + def link_to_unless_current(name, options = {}, html_options = {}, &block) + link_to_unless current_page?(options), name, options, html_options, &block end # Creates a link tag of the given +name+ using a URL created by the set of @@ -246,15 +268,15 @@ module ActionView # # => Reply # # If not... # # => Reply - def link_to_unless(condition, name, options = {}, html_options = {}, *parameters_for_method_reference, &block) + def link_to_unless(condition, name, options = {}, html_options = {}, &block) if condition if block_given? - block.arity <= 1 ? yield(name) : yield(name, options, html_options, *parameters_for_method_reference) + block.arity <= 1 ? yield(name) : yield(name, options, html_options) else name end else - link_to(name, options, html_options, *parameters_for_method_reference) + link_to(name, options, html_options) end end @@ -278,8 +300,8 @@ module ActionView # # => Login # # If they are logged in... # # => my_username - def link_to_if(condition, name, options = {}, html_options = {}, *parameters_for_method_reference, &block) - link_to_unless !condition, name, options, html_options, *parameters_for_method_reference, &block + def link_to_if(condition, name, options = {}, html_options = {}, &block) + link_to_unless !condition, name, options, html_options, &block end # Creates a mailto link tag to the specified +email_address+, which is -- cgit v1.2.3