Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added to_param call for parameters when composing an url using url_fo…

…r from something else than strings #812 [Sam Stephenson]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@935 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 7e745f3aa5de4e117758ee92e2ca92aa265c6deb 1 parent 6cd3bda
David Heinemeier Hansson dhh authored
21 actionpack/CHANGELOG
View
@@ -1,5 +1,26 @@
*SVN*
+* Added to_param call for parameters when composing an url using url_for from something else than strings #812 [Sam Stephenson]. Example:
+
+ class Page
+   def initialize(number)
+     @number = number
+   end
+   # ...
+   def to_param
+     @number.to_s
+   end
+ end
+
+ You can now use instances of Page with url_for:
+
+ class BarController < ApplicationController
+   def baz
+     page = Page.new(4)
+     url = url_for :page => page # => "http://foo/bar/baz?page=4"
+   end
+ end
+
* Fixed form helpers to query Model#id_before_type_cast instead of Model#id as a temporary workaround for Ruby 1.8.2 warnings #818 [DeLynn B]
* Fixed TextHelper#markdown to use blank? instead of empty? so it can deal with nil strings passed #814 [Johan Sörensen]
9 actionpack/lib/action_controller/routing.rb
View
@@ -64,9 +64,9 @@ def generate(options, defaults={})
if value.nil? || item == :controller
value
elsif collection
- CGI.escape(value.to_s).gsub(/%2F/, "/")
+ Routing.extract_parameter_value(value).gsub(/%2F/, "/")
else
- CGI.escape(value.to_s)
+ Routing.extract_parameter_value(value)
end
else
item
@@ -316,6 +316,11 @@ def draw
end
end
+ def self.extract_parameter_value(parameter)
+ value = parameter.respond_to?(:to_param) ? parameter.to_param : parameter.to_s
+ CGI.escape(value)
+ end
+
def self.draw(*args, &block) #:nodoc:
Routes.draw(*args) {|*args| block.call(*args)}
end
2  actionpack/lib/action_controller/url_rewriter.rb
View
@@ -95,7 +95,7 @@ def build_query_string(hash)
key = CGI.escape key
key += '[]' if value.class == Array
value = [ value ] unless value.class == Array
- value.each { |val| elements << "#{key}=#{CGI.escape(val.to_s)}" }
+ value.each { |val| elements << "#{key}=#{Routing.extract_parameter_value(val)}" }
end
query_string << ("?" + elements.join("&")) unless elements.empty?
Please sign in to comment.
Something went wrong with that request. Please try again.