Created
August 29, 2014 06:53
-
-
Save sj26/44ef47fe8b98b46ee32d to your computer and use it in GitHub Desktop.
Rails 4's resource parameter name option for Rails 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# From https://stackoverflow.com/questions/6592038/change-the-name-of-the-id-parameter-in-routing-resources-for-rails3/13427336#13427336 | |
# | |
# Unfortuantely, in Rails 3, the :key option for resources was | |
# removed, so you can no longer easily change the name for routes | |
# created in this way by just passing in an extra option. | |
# | |
# All is not lost, however; it seems in Rails 4, the :param option | |
# has been added, which seems to do exactly what you're looking for. | |
# You can take a look at the Rails 3 code compared to the Rails 4 | |
# code. | |
# | |
# Rails 3: https://github.com/rails/rails/blob/e359e3ab935d6790d9a1181e6ebe0038ad168b94/actionpack/lib/action_dispatch/routing/mapper.rb#L867 | |
# | |
# Rails 4: https://github.com/rails/rails/blob/e2334a249aa7b9e18d684fd5cf213e78053a9222/actionpack/lib/action_dispatch/routing/mapper.rb#L1020 | |
# | |
# This patch backports the :param option. | |
# | |
class ActionDispatch::Routing::Mapper::Resources::Resource | |
# overridden: | |
def member_scope | |
"#{path}/:#{param}" | |
end | |
def nested_scope | |
"#{path}/:#{nested_param}" | |
end | |
# backported: | |
def param | |
@param ||= (@options[:param] || :id).to_sym | |
end | |
def nested_param | |
:"#{singular}_#{param}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had to add :param to ActionDispatch::Routing::Mapper::Resources::RESOURCE_OPTIONS to get this patch to work: https://github.com/rails/rails/blob/e359e3ab935d6790d9a1181e6ebe0038ad168b94/actionpack/lib/action_dispatch/routing/mapper.rb#L861
Not the cleanest option but a quick fix: