aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/conversion.rb
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2014-02-04 10:27:46 +0100
committerYves Senn <yves.senn@gmail.com>2014-02-04 10:27:46 +0100
commit7d196cf360321466c0eefc474bfad1be7e3ea7ab (patch)
treebf4f13bf0333195f5a648736266d27efda0f31fc /activemodel/lib/active_model/conversion.rb
parenta5dd1d249010694be1af905e5c1bc9ee05ca7119 (diff)
downloadrails-7d196cf360321466c0eefc474bfad1be7e3ea7ab.tar.gz
rails-7d196cf360321466c0eefc474bfad1be7e3ea7ab.tar.bz2
rails-7d196cf360321466c0eefc474bfad1be7e3ea7ab.zip
`#to_param` returns `nil` if `to_key` returns `nil`. Closes #11399.
The documentation of `#to_key` (http://api.rubyonrails.org/classes/ActiveModel/Conversion.html#method-i-to_key) states that it returns `nil` if there are no key attributes. `to_param` needs to be aware of that fact and return `nil` as well. Previously it raised the following exception: ``` 1) Error: ConversionTest#test_to_param_returns_nil_if_to_key_is_nil: NoMethodError: undefined method `join' for nil:NilClass /Users/senny/Projects/rails/activemodel/lib/active_model/conversion.rb:65:in `to_param' /Users/senny/Projects/rails/activemodel/test/cases/conversion_test.rb:34:in `block in <class:ConversionTest>' ```
Diffstat (limited to 'activemodel/lib/active_model/conversion.rb')
-rw-r--r--activemodel/lib/active_model/conversion.rb2
1 files changed, 1 insertions, 1 deletions
diff --git a/activemodel/lib/active_model/conversion.rb b/activemodel/lib/active_model/conversion.rb
index 6b0a752566..0a19ef686d 100644
--- a/activemodel/lib/active_model/conversion.rb
+++ b/activemodel/lib/active_model/conversion.rb
@@ -62,7 +62,7 @@ module ActiveModel
# person = Person.create
# person.to_param # => "1"
def to_param
- persisted? ? to_key.join('-') : nil
+ (persisted? && key = to_key) ? key.join('-') : nil
end
# Returns a +string+ identifying the path associated with the object.